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.
1 """Data structures configuring Black behavior.
3 Mostly around Python language feature support per version and Black configuration
7 from dataclasses import dataclass, field
8 from enum import Enum, auto
9 from hashlib import sha256
10 from operator import attrgetter
11 from typing import Dict, Final, Set
12 from warnings import warn
14 from black.const import DEFAULT_LINE_LENGTH
17 class TargetVersion(Enum):
32 NUMERIC_UNDERSCORES = 3
33 TRAILING_COMMA_IN_CALL = 4
34 TRAILING_COMMA_IN_DEF = 5
35 # The following two feature-flags are mutually exclusive, and exactly one should be
36 # set for every version of python.
39 ASSIGNMENT_EXPRESSIONS = 8
40 POS_ONLY_ARGUMENTS = 9
41 RELAXED_DECORATORS = 10
43 UNPACKING_ON_FLOW = 12
44 ANN_ASSIGN_EXTENDED_RHS = 13
46 VARIADIC_GENERICS = 15
48 PARENTHESIZED_CONTEXT_MANAGERS = 17
50 FORCE_OPTIONAL_PARENTHESES = 50
53 FUTURE_ANNOTATIONS = 51
56 FUTURE_FLAG_TO_FEATURE: Final = {
57 "annotations": Feature.FUTURE_ANNOTATIONS,
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},
67 Feature.NUMERIC_UNDERSCORES,
68 Feature.TRAILING_COMMA_IN_CALL,
69 Feature.TRAILING_COMMA_IN_DEF,
70 Feature.ASYNC_IDENTIFIERS,
74 Feature.NUMERIC_UNDERSCORES,
75 Feature.TRAILING_COMMA_IN_CALL,
76 Feature.TRAILING_COMMA_IN_DEF,
77 Feature.ASYNC_KEYWORDS,
78 Feature.FUTURE_ANNOTATIONS,
82 Feature.DEBUG_F_STRINGS,
83 Feature.NUMERIC_UNDERSCORES,
84 Feature.TRAILING_COMMA_IN_CALL,
85 Feature.TRAILING_COMMA_IN_DEF,
86 Feature.ASYNC_KEYWORDS,
87 Feature.FUTURE_ANNOTATIONS,
88 Feature.ASSIGNMENT_EXPRESSIONS,
89 Feature.POS_ONLY_ARGUMENTS,
90 Feature.UNPACKING_ON_FLOW,
91 Feature.ANN_ASSIGN_EXTENDED_RHS,
95 Feature.DEBUG_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 Feature.PARENTHESIZED_CONTEXT_MANAGERS,
108 TargetVersion.PY310: {
110 Feature.DEBUG_F_STRINGS,
111 Feature.NUMERIC_UNDERSCORES,
112 Feature.TRAILING_COMMA_IN_CALL,
113 Feature.TRAILING_COMMA_IN_DEF,
114 Feature.ASYNC_KEYWORDS,
115 Feature.FUTURE_ANNOTATIONS,
116 Feature.ASSIGNMENT_EXPRESSIONS,
117 Feature.RELAXED_DECORATORS,
118 Feature.POS_ONLY_ARGUMENTS,
119 Feature.UNPACKING_ON_FLOW,
120 Feature.ANN_ASSIGN_EXTENDED_RHS,
121 Feature.PARENTHESIZED_CONTEXT_MANAGERS,
122 Feature.PATTERN_MATCHING,
124 TargetVersion.PY311: {
126 Feature.DEBUG_F_STRINGS,
127 Feature.NUMERIC_UNDERSCORES,
128 Feature.TRAILING_COMMA_IN_CALL,
129 Feature.TRAILING_COMMA_IN_DEF,
130 Feature.ASYNC_KEYWORDS,
131 Feature.FUTURE_ANNOTATIONS,
132 Feature.ASSIGNMENT_EXPRESSIONS,
133 Feature.RELAXED_DECORATORS,
134 Feature.POS_ONLY_ARGUMENTS,
135 Feature.UNPACKING_ON_FLOW,
136 Feature.ANN_ASSIGN_EXTENDED_RHS,
137 Feature.PARENTHESIZED_CONTEXT_MANAGERS,
138 Feature.PATTERN_MATCHING,
140 Feature.VARIADIC_GENERICS,
142 TargetVersion.PY312: {
144 Feature.DEBUG_F_STRINGS,
145 Feature.NUMERIC_UNDERSCORES,
146 Feature.TRAILING_COMMA_IN_CALL,
147 Feature.TRAILING_COMMA_IN_DEF,
148 Feature.ASYNC_KEYWORDS,
149 Feature.FUTURE_ANNOTATIONS,
150 Feature.ASSIGNMENT_EXPRESSIONS,
151 Feature.RELAXED_DECORATORS,
152 Feature.POS_ONLY_ARGUMENTS,
153 Feature.UNPACKING_ON_FLOW,
154 Feature.ANN_ASSIGN_EXTENDED_RHS,
155 Feature.PARENTHESIZED_CONTEXT_MANAGERS,
156 Feature.PATTERN_MATCHING,
158 Feature.VARIADIC_GENERICS,
164 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
165 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
169 """Individual preview style features."""
171 add_trailing_comma_consistently = auto()
172 blank_line_after_nested_stub_class = auto()
173 hex_codes_in_unicode_sequences = auto()
174 improved_async_statements_handling = auto()
175 multiline_string_handling = auto()
176 no_blank_line_before_class_docstring = auto()
177 prefer_splitting_right_hand_side_of_assignments = auto()
178 # NOTE: string_processing requires wrap_long_dict_values_in_parens
179 # for https://github.com/psf/black/issues/3117 to be fixed.
180 string_processing = auto()
181 parenthesize_conditional_expressions = auto()
182 skip_magic_trailing_comma_in_subscript = auto()
183 wrap_long_dict_values_in_parens = auto()
184 wrap_multiple_context_managers_in_parens = auto()
187 class Deprecated(UserWarning):
188 """Visible deprecation warning."""
193 target_versions: Set[TargetVersion] = field(default_factory=set)
194 line_length: int = DEFAULT_LINE_LENGTH
195 string_normalization: bool = True
197 is_ipynb: bool = False
198 skip_source_first_line: bool = False
199 magic_trailing_comma: bool = True
200 experimental_string_processing: bool = False
201 python_cell_magics: Set[str] = field(default_factory=set)
202 preview: bool = False
204 def __post_init__(self) -> None:
205 if self.experimental_string_processing:
207 "`experimental string processing` has been included in `preview`"
208 " and deprecated. Use `preview` instead.",
212 def __contains__(self, feature: Preview) -> bool:
214 Provide `Preview.FEATURE in Mode` syntax that mirrors the ``preview`` flag.
216 The argument is not checked and features are not differentiated.
217 They only exist to make development easier by clarifying intent.
219 if feature is Preview.string_processing:
220 return self.preview or self.experimental_string_processing
223 def get_cache_key(self) -> str:
224 if self.target_versions:
225 version_str = ",".join(
227 for version in sorted(self.target_versions, key=attrgetter("value"))
233 str(self.line_length),
234 str(int(self.string_normalization)),
235 str(int(self.is_pyi)),
236 str(int(self.is_ipynb)),
237 str(int(self.skip_source_first_line)),
238 str(int(self.magic_trailing_comma)),
239 str(int(self.experimental_string_processing)),
240 str(int(self.preview)),
241 sha256((",".join(sorted(self.python_cell_magics))).encode()).hexdigest(),
243 return ".".join(parts)