]> 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:

32b65d16ca5a5f1e59543a3a278f4bff3e91f22b
[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 import sys
8 from dataclasses import dataclass, field
9 from enum import Enum, auto
10 from hashlib import sha256
11 from operator import attrgetter
12 from typing import Dict, Set
13 from warnings import warn
14
15 if sys.version_info < (3, 8):
16     from typing_extensions import Final
17 else:
18     from typing import Final
19
20 from black.const import DEFAULT_LINE_LENGTH
21
22
23 class TargetVersion(Enum):
24     PY33 = 3
25     PY34 = 4
26     PY35 = 5
27     PY36 = 6
28     PY37 = 7
29     PY38 = 8
30     PY39 = 9
31     PY310 = 10
32     PY311 = 11
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     EXCEPT_STAR = 14
51     VARIADIC_GENERICS = 15
52     FORCE_OPTIONAL_PARENTHESES = 50
53
54     # __future__ flags
55     FUTURE_ANNOTATIONS = 51
56
57
58 FUTURE_FLAG_TO_FEATURE: Final = {
59     "annotations": Feature.FUTURE_ANNOTATIONS,
60 }
61
62
63 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
64     TargetVersion.PY33: {Feature.ASYNC_IDENTIFIERS},
65     TargetVersion.PY34: {Feature.ASYNC_IDENTIFIERS},
66     TargetVersion.PY35: {Feature.TRAILING_COMMA_IN_CALL, Feature.ASYNC_IDENTIFIERS},
67     TargetVersion.PY36: {
68         Feature.F_STRINGS,
69         Feature.NUMERIC_UNDERSCORES,
70         Feature.TRAILING_COMMA_IN_CALL,
71         Feature.TRAILING_COMMA_IN_DEF,
72         Feature.ASYNC_IDENTIFIERS,
73     },
74     TargetVersion.PY37: {
75         Feature.F_STRINGS,
76         Feature.NUMERIC_UNDERSCORES,
77         Feature.TRAILING_COMMA_IN_CALL,
78         Feature.TRAILING_COMMA_IN_DEF,
79         Feature.ASYNC_KEYWORDS,
80         Feature.FUTURE_ANNOTATIONS,
81     },
82     TargetVersion.PY38: {
83         Feature.F_STRINGS,
84         Feature.NUMERIC_UNDERSCORES,
85         Feature.TRAILING_COMMA_IN_CALL,
86         Feature.TRAILING_COMMA_IN_DEF,
87         Feature.ASYNC_KEYWORDS,
88         Feature.FUTURE_ANNOTATIONS,
89         Feature.ASSIGNMENT_EXPRESSIONS,
90         Feature.POS_ONLY_ARGUMENTS,
91         Feature.UNPACKING_ON_FLOW,
92         Feature.ANN_ASSIGN_EXTENDED_RHS,
93     },
94     TargetVersion.PY39: {
95         Feature.F_STRINGS,
96         Feature.NUMERIC_UNDERSCORES,
97         Feature.TRAILING_COMMA_IN_CALL,
98         Feature.TRAILING_COMMA_IN_DEF,
99         Feature.ASYNC_KEYWORDS,
100         Feature.FUTURE_ANNOTATIONS,
101         Feature.ASSIGNMENT_EXPRESSIONS,
102         Feature.RELAXED_DECORATORS,
103         Feature.POS_ONLY_ARGUMENTS,
104         Feature.UNPACKING_ON_FLOW,
105         Feature.ANN_ASSIGN_EXTENDED_RHS,
106     },
107     TargetVersion.PY310: {
108         Feature.F_STRINGS,
109         Feature.NUMERIC_UNDERSCORES,
110         Feature.TRAILING_COMMA_IN_CALL,
111         Feature.TRAILING_COMMA_IN_DEF,
112         Feature.ASYNC_KEYWORDS,
113         Feature.FUTURE_ANNOTATIONS,
114         Feature.ASSIGNMENT_EXPRESSIONS,
115         Feature.RELAXED_DECORATORS,
116         Feature.POS_ONLY_ARGUMENTS,
117         Feature.UNPACKING_ON_FLOW,
118         Feature.ANN_ASSIGN_EXTENDED_RHS,
119         Feature.PATTERN_MATCHING,
120     },
121     TargetVersion.PY311: {
122         Feature.F_STRINGS,
123         Feature.NUMERIC_UNDERSCORES,
124         Feature.TRAILING_COMMA_IN_CALL,
125         Feature.TRAILING_COMMA_IN_DEF,
126         Feature.ASYNC_KEYWORDS,
127         Feature.FUTURE_ANNOTATIONS,
128         Feature.ASSIGNMENT_EXPRESSIONS,
129         Feature.RELAXED_DECORATORS,
130         Feature.POS_ONLY_ARGUMENTS,
131         Feature.UNPACKING_ON_FLOW,
132         Feature.ANN_ASSIGN_EXTENDED_RHS,
133         Feature.PATTERN_MATCHING,
134         Feature.EXCEPT_STAR,
135         Feature.VARIADIC_GENERICS,
136     },
137 }
138
139
140 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
141     return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
142
143
144 class Preview(Enum):
145     """Individual preview style features."""
146
147     annotation_parens = auto()
148     long_docstring_quotes_on_newline = auto()
149     normalize_docstring_quotes_and_prefixes_properly = auto()
150     one_element_subscript = auto()
151     remove_block_trailing_newline = auto()
152     remove_redundant_parens = auto()
153     string_processing = auto()
154
155
156 class Deprecated(UserWarning):
157     """Visible deprecation warning."""
158
159
160 @dataclass
161 class Mode:
162     target_versions: Set[TargetVersion] = field(default_factory=set)
163     line_length: int = DEFAULT_LINE_LENGTH
164     string_normalization: bool = True
165     is_pyi: bool = False
166     is_ipynb: bool = False
167     magic_trailing_comma: bool = True
168     experimental_string_processing: bool = False
169     python_cell_magics: Set[str] = field(default_factory=set)
170     preview: bool = False
171
172     def __post_init__(self) -> None:
173         if self.experimental_string_processing:
174             warn(
175                 "`experimental string processing` has been included in `preview`"
176                 " and deprecated. Use `preview` instead.",
177                 Deprecated,
178             )
179
180     def __contains__(self, feature: Preview) -> bool:
181         """
182         Provide `Preview.FEATURE in Mode` syntax that mirrors the ``preview`` flag.
183
184         The argument is not checked and features are not differentiated.
185         They only exist to make development easier by clarifying intent.
186         """
187         if feature is Preview.string_processing:
188             return self.preview or self.experimental_string_processing
189         return self.preview
190
191     def get_cache_key(self) -> str:
192         if self.target_versions:
193             version_str = ",".join(
194                 str(version.value)
195                 for version in sorted(self.target_versions, key=attrgetter("value"))
196             )
197         else:
198             version_str = "-"
199         parts = [
200             version_str,
201             str(self.line_length),
202             str(int(self.string_normalization)),
203             str(int(self.is_pyi)),
204             str(int(self.is_ipynb)),
205             str(int(self.magic_trailing_comma)),
206             str(int(self.experimental_string_processing)),
207             str(int(self.preview)),
208             sha256((",".join(sorted(self.python_cell_magics))).encode()).hexdigest(),
209         ]
210         return ".".join(parts)