]> git.madduck.net Git - etc/vim.git/blob - src/black/mode.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Update classifiers to reflect stable (#2823)
[etc/vim.git] / src / black / mode.py
1 """Data structures configuring Black behavior.
2
3 Mostly around Python language feature support per version and Black configuration
4 chosen by the user.
5 """
6
7 from hashlib import md5
8 import sys
9
10 from dataclasses import dataclass, field
11 from enum import Enum, auto
12 from operator import attrgetter
13 from typing import Dict, Set
14 from warnings import warn
15
16 if sys.version_info < (3, 8):
17     from typing_extensions import Final
18 else:
19     from typing import Final
20
21 from black.const import DEFAULT_LINE_LENGTH
22
23
24 class TargetVersion(Enum):
25     PY33 = 3
26     PY34 = 4
27     PY35 = 5
28     PY36 = 6
29     PY37 = 7
30     PY38 = 8
31     PY39 = 9
32     PY310 = 10
33
34
35 class Feature(Enum):
36     F_STRINGS = 2
37     NUMERIC_UNDERSCORES = 3
38     TRAILING_COMMA_IN_CALL = 4
39     TRAILING_COMMA_IN_DEF = 5
40     # The following two feature-flags are mutually exclusive, and exactly one should be
41     # set for every version of python.
42     ASYNC_IDENTIFIERS = 6
43     ASYNC_KEYWORDS = 7
44     ASSIGNMENT_EXPRESSIONS = 8
45     POS_ONLY_ARGUMENTS = 9
46     RELAXED_DECORATORS = 10
47     PATTERN_MATCHING = 11
48     UNPACKING_ON_FLOW = 12
49     ANN_ASSIGN_EXTENDED_RHS = 13
50     FORCE_OPTIONAL_PARENTHESES = 50
51
52     # __future__ flags
53     FUTURE_ANNOTATIONS = 51
54
55
56 FUTURE_FLAG_TO_FEATURE: Final = {
57     "annotations": Feature.FUTURE_ANNOTATIONS,
58 }
59
60
61 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
62     TargetVersion.PY33: {Feature.ASYNC_IDENTIFIERS},
63     TargetVersion.PY34: {Feature.ASYNC_IDENTIFIERS},
64     TargetVersion.PY35: {Feature.TRAILING_COMMA_IN_CALL, Feature.ASYNC_IDENTIFIERS},
65     TargetVersion.PY36: {
66         Feature.F_STRINGS,
67         Feature.NUMERIC_UNDERSCORES,
68         Feature.TRAILING_COMMA_IN_CALL,
69         Feature.TRAILING_COMMA_IN_DEF,
70         Feature.ASYNC_IDENTIFIERS,
71     },
72     TargetVersion.PY37: {
73         Feature.F_STRINGS,
74         Feature.NUMERIC_UNDERSCORES,
75         Feature.TRAILING_COMMA_IN_CALL,
76         Feature.TRAILING_COMMA_IN_DEF,
77         Feature.ASYNC_KEYWORDS,
78         Feature.FUTURE_ANNOTATIONS,
79     },
80     TargetVersion.PY38: {
81         Feature.F_STRINGS,
82         Feature.NUMERIC_UNDERSCORES,
83         Feature.TRAILING_COMMA_IN_CALL,
84         Feature.TRAILING_COMMA_IN_DEF,
85         Feature.ASYNC_KEYWORDS,
86         Feature.FUTURE_ANNOTATIONS,
87         Feature.ASSIGNMENT_EXPRESSIONS,
88         Feature.POS_ONLY_ARGUMENTS,
89         Feature.UNPACKING_ON_FLOW,
90         Feature.ANN_ASSIGN_EXTENDED_RHS,
91     },
92     TargetVersion.PY39: {
93         Feature.F_STRINGS,
94         Feature.NUMERIC_UNDERSCORES,
95         Feature.TRAILING_COMMA_IN_CALL,
96         Feature.TRAILING_COMMA_IN_DEF,
97         Feature.ASYNC_KEYWORDS,
98         Feature.FUTURE_ANNOTATIONS,
99         Feature.ASSIGNMENT_EXPRESSIONS,
100         Feature.RELAXED_DECORATORS,
101         Feature.POS_ONLY_ARGUMENTS,
102         Feature.UNPACKING_ON_FLOW,
103         Feature.ANN_ASSIGN_EXTENDED_RHS,
104     },
105     TargetVersion.PY310: {
106         Feature.F_STRINGS,
107         Feature.NUMERIC_UNDERSCORES,
108         Feature.TRAILING_COMMA_IN_CALL,
109         Feature.TRAILING_COMMA_IN_DEF,
110         Feature.ASYNC_KEYWORDS,
111         Feature.FUTURE_ANNOTATIONS,
112         Feature.ASSIGNMENT_EXPRESSIONS,
113         Feature.RELAXED_DECORATORS,
114         Feature.POS_ONLY_ARGUMENTS,
115         Feature.UNPACKING_ON_FLOW,
116         Feature.ANN_ASSIGN_EXTENDED_RHS,
117         Feature.PATTERN_MATCHING,
118     },
119 }
120
121
122 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
123     return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
124
125
126 class Preview(Enum):
127     """Individual preview style features."""
128
129     string_processing = auto()
130     hug_simple_powers = auto()
131
132
133 class Deprecated(UserWarning):
134     """Visible deprecation warning."""
135
136
137 @dataclass
138 class Mode:
139     target_versions: Set[TargetVersion] = field(default_factory=set)
140     line_length: int = DEFAULT_LINE_LENGTH
141     string_normalization: bool = True
142     is_pyi: bool = False
143     is_ipynb: bool = False
144     magic_trailing_comma: bool = True
145     experimental_string_processing: bool = False
146     python_cell_magics: Set[str] = field(default_factory=set)
147     preview: bool = False
148
149     def __post_init__(self) -> None:
150         if self.experimental_string_processing:
151             warn(
152                 "`experimental string processing` has been included in `preview`"
153                 " and deprecated. Use `preview` instead.",
154                 Deprecated,
155             )
156
157     def __contains__(self, feature: Preview) -> bool:
158         """
159         Provide `Preview.FEATURE in Mode` syntax that mirrors the ``preview`` flag.
160
161         The argument is not checked and features are not differentiated.
162         They only exist to make development easier by clarifying intent.
163         """
164         if feature is Preview.string_processing:
165             return self.preview or self.experimental_string_processing
166         return self.preview
167
168     def get_cache_key(self) -> str:
169         if self.target_versions:
170             version_str = ",".join(
171                 str(version.value)
172                 for version in sorted(self.target_versions, key=attrgetter("value"))
173             )
174         else:
175             version_str = "-"
176         parts = [
177             version_str,
178             str(self.line_length),
179             str(int(self.string_normalization)),
180             str(int(self.is_pyi)),
181             str(int(self.is_ipynb)),
182             str(int(self.magic_trailing_comma)),
183             str(int(self.experimental_string_processing)),
184             str(int(self.preview)),
185             md5((",".join(sorted(self.python_cell_magics))).encode()).hexdigest(),
186         ]
187         return ".".join(parts)