]> 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 document now that paren wrapping CMs on Python 3.9+ is implemented (#3520)
[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     DEBUG_F_STRINGS = 16
53     PARENTHESIZED_CONTEXT_MANAGERS = 17
54     FORCE_OPTIONAL_PARENTHESES = 50
55
56     # __future__ flags
57     FUTURE_ANNOTATIONS = 51
58
59
60 FUTURE_FLAG_TO_FEATURE: Final = {
61     "annotations": Feature.FUTURE_ANNOTATIONS,
62 }
63
64
65 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
66     TargetVersion.PY33: {Feature.ASYNC_IDENTIFIERS},
67     TargetVersion.PY34: {Feature.ASYNC_IDENTIFIERS},
68     TargetVersion.PY35: {Feature.TRAILING_COMMA_IN_CALL, Feature.ASYNC_IDENTIFIERS},
69     TargetVersion.PY36: {
70         Feature.F_STRINGS,
71         Feature.NUMERIC_UNDERSCORES,
72         Feature.TRAILING_COMMA_IN_CALL,
73         Feature.TRAILING_COMMA_IN_DEF,
74         Feature.ASYNC_IDENTIFIERS,
75     },
76     TargetVersion.PY37: {
77         Feature.F_STRINGS,
78         Feature.NUMERIC_UNDERSCORES,
79         Feature.TRAILING_COMMA_IN_CALL,
80         Feature.TRAILING_COMMA_IN_DEF,
81         Feature.ASYNC_KEYWORDS,
82         Feature.FUTURE_ANNOTATIONS,
83     },
84     TargetVersion.PY38: {
85         Feature.F_STRINGS,
86         Feature.DEBUG_F_STRINGS,
87         Feature.NUMERIC_UNDERSCORES,
88         Feature.TRAILING_COMMA_IN_CALL,
89         Feature.TRAILING_COMMA_IN_DEF,
90         Feature.ASYNC_KEYWORDS,
91         Feature.FUTURE_ANNOTATIONS,
92         Feature.ASSIGNMENT_EXPRESSIONS,
93         Feature.POS_ONLY_ARGUMENTS,
94         Feature.UNPACKING_ON_FLOW,
95         Feature.ANN_ASSIGN_EXTENDED_RHS,
96     },
97     TargetVersion.PY39: {
98         Feature.F_STRINGS,
99         Feature.DEBUG_F_STRINGS,
100         Feature.NUMERIC_UNDERSCORES,
101         Feature.TRAILING_COMMA_IN_CALL,
102         Feature.TRAILING_COMMA_IN_DEF,
103         Feature.ASYNC_KEYWORDS,
104         Feature.FUTURE_ANNOTATIONS,
105         Feature.ASSIGNMENT_EXPRESSIONS,
106         Feature.RELAXED_DECORATORS,
107         Feature.POS_ONLY_ARGUMENTS,
108         Feature.UNPACKING_ON_FLOW,
109         Feature.ANN_ASSIGN_EXTENDED_RHS,
110         Feature.PARENTHESIZED_CONTEXT_MANAGERS,
111     },
112     TargetVersion.PY310: {
113         Feature.F_STRINGS,
114         Feature.DEBUG_F_STRINGS,
115         Feature.NUMERIC_UNDERSCORES,
116         Feature.TRAILING_COMMA_IN_CALL,
117         Feature.TRAILING_COMMA_IN_DEF,
118         Feature.ASYNC_KEYWORDS,
119         Feature.FUTURE_ANNOTATIONS,
120         Feature.ASSIGNMENT_EXPRESSIONS,
121         Feature.RELAXED_DECORATORS,
122         Feature.POS_ONLY_ARGUMENTS,
123         Feature.UNPACKING_ON_FLOW,
124         Feature.ANN_ASSIGN_EXTENDED_RHS,
125         Feature.PARENTHESIZED_CONTEXT_MANAGERS,
126         Feature.PATTERN_MATCHING,
127     },
128     TargetVersion.PY311: {
129         Feature.F_STRINGS,
130         Feature.DEBUG_F_STRINGS,
131         Feature.NUMERIC_UNDERSCORES,
132         Feature.TRAILING_COMMA_IN_CALL,
133         Feature.TRAILING_COMMA_IN_DEF,
134         Feature.ASYNC_KEYWORDS,
135         Feature.FUTURE_ANNOTATIONS,
136         Feature.ASSIGNMENT_EXPRESSIONS,
137         Feature.RELAXED_DECORATORS,
138         Feature.POS_ONLY_ARGUMENTS,
139         Feature.UNPACKING_ON_FLOW,
140         Feature.ANN_ASSIGN_EXTENDED_RHS,
141         Feature.PARENTHESIZED_CONTEXT_MANAGERS,
142         Feature.PATTERN_MATCHING,
143         Feature.EXCEPT_STAR,
144         Feature.VARIADIC_GENERICS,
145     },
146 }
147
148
149 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
150     return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
151
152
153 class Preview(Enum):
154     """Individual preview style features."""
155
156     hex_codes_in_unicode_sequences = auto()
157     annotation_parens = auto()
158     empty_lines_before_class_or_def_with_leading_comments = auto()
159     handle_trailing_commas_in_head = auto()
160     long_docstring_quotes_on_newline = auto()
161     normalize_docstring_quotes_and_prefixes_properly = auto()
162     one_element_subscript = auto()
163     prefer_splitting_right_hand_side_of_assignments = auto()
164     remove_block_trailing_newline = auto()
165     remove_redundant_parens = auto()
166     # NOTE: string_processing requires wrap_long_dict_values_in_parens
167     # for https://github.com/psf/black/issues/3117 to be fixed.
168     string_processing = auto()
169     parenthesize_conditional_expressions = auto()
170     skip_magic_trailing_comma_in_subscript = auto()
171     wrap_long_dict_values_in_parens = auto()
172     wrap_multiple_context_managers_in_parens = auto()
173
174
175 class Deprecated(UserWarning):
176     """Visible deprecation warning."""
177
178
179 @dataclass
180 class Mode:
181     target_versions: Set[TargetVersion] = field(default_factory=set)
182     line_length: int = DEFAULT_LINE_LENGTH
183     string_normalization: bool = True
184     is_pyi: bool = False
185     is_ipynb: bool = False
186     skip_source_first_line: bool = False
187     magic_trailing_comma: bool = True
188     experimental_string_processing: bool = False
189     python_cell_magics: Set[str] = field(default_factory=set)
190     preview: bool = False
191
192     def __post_init__(self) -> None:
193         if self.experimental_string_processing:
194             warn(
195                 (
196                     "`experimental string processing` has been included in `preview`"
197                     " and deprecated. Use `preview` instead."
198                 ),
199                 Deprecated,
200             )
201
202     def __contains__(self, feature: Preview) -> bool:
203         """
204         Provide `Preview.FEATURE in Mode` syntax that mirrors the ``preview`` flag.
205
206         The argument is not checked and features are not differentiated.
207         They only exist to make development easier by clarifying intent.
208         """
209         if feature is Preview.string_processing:
210             return self.preview or self.experimental_string_processing
211         return self.preview
212
213     def get_cache_key(self) -> str:
214         if self.target_versions:
215             version_str = ",".join(
216                 str(version.value)
217                 for version in sorted(self.target_versions, key=attrgetter("value"))
218             )
219         else:
220             version_str = "-"
221         parts = [
222             version_str,
223             str(self.line_length),
224             str(int(self.string_normalization)),
225             str(int(self.is_pyi)),
226             str(int(self.is_ipynb)),
227             str(int(self.skip_source_first_line)),
228             str(int(self.magic_trailing_comma)),
229             str(int(self.experimental_string_processing)),
230             str(int(self.preview)),
231             sha256((",".join(sorted(self.python_cell_magics))).encode()).hexdigest(),
232         ]
233         return ".".join(parts)