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 hashlib import sha256
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
16 if sys.version_info < (3, 8):
17 from typing_extensions import Final
19 from typing import Final
21 from black.const import DEFAULT_LINE_LENGTH
24 class TargetVersion(Enum):
38 NUMERIC_UNDERSCORES = 3
39 TRAILING_COMMA_IN_CALL = 4
40 TRAILING_COMMA_IN_DEF = 5
41 # The following two feature-flags are mutually exclusive, and exactly one should be
42 # set for every version of python.
45 ASSIGNMENT_EXPRESSIONS = 8
46 POS_ONLY_ARGUMENTS = 9
47 RELAXED_DECORATORS = 10
49 UNPACKING_ON_FLOW = 12
50 ANN_ASSIGN_EXTENDED_RHS = 13
52 FORCE_OPTIONAL_PARENTHESES = 50
55 FUTURE_ANNOTATIONS = 51
58 FUTURE_FLAG_TO_FEATURE: Final = {
59 "annotations": Feature.FUTURE_ANNOTATIONS,
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},
69 Feature.NUMERIC_UNDERSCORES,
70 Feature.TRAILING_COMMA_IN_CALL,
71 Feature.TRAILING_COMMA_IN_DEF,
72 Feature.ASYNC_IDENTIFIERS,
76 Feature.NUMERIC_UNDERSCORES,
77 Feature.TRAILING_COMMA_IN_CALL,
78 Feature.TRAILING_COMMA_IN_DEF,
79 Feature.ASYNC_KEYWORDS,
80 Feature.FUTURE_ANNOTATIONS,
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,
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,
107 TargetVersion.PY310: {
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,
121 TargetVersion.PY311: {
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,
139 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
140 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
144 """Individual preview style features."""
146 string_processing = auto()
147 remove_redundant_parens = auto()
148 one_element_subscript = auto()
149 annotation_parens = auto()
150 long_docstring_quotes_on_newline = auto()
153 class Deprecated(UserWarning):
154 """Visible deprecation warning."""
159 target_versions: Set[TargetVersion] = field(default_factory=set)
160 line_length: int = DEFAULT_LINE_LENGTH
161 string_normalization: bool = True
163 is_ipynb: bool = False
164 magic_trailing_comma: bool = True
165 experimental_string_processing: bool = False
166 python_cell_magics: Set[str] = field(default_factory=set)
167 preview: bool = False
169 def __post_init__(self) -> None:
170 if self.experimental_string_processing:
172 "`experimental string processing` has been included in `preview`"
173 " and deprecated. Use `preview` instead.",
177 def __contains__(self, feature: Preview) -> bool:
179 Provide `Preview.FEATURE in Mode` syntax that mirrors the ``preview`` flag.
181 The argument is not checked and features are not differentiated.
182 They only exist to make development easier by clarifying intent.
184 if feature is Preview.string_processing:
185 return self.preview or self.experimental_string_processing
188 def get_cache_key(self) -> str:
189 if self.target_versions:
190 version_str = ",".join(
192 for version in sorted(self.target_versions, key=attrgetter("value"))
198 str(self.line_length),
199 str(int(self.string_normalization)),
200 str(int(self.is_pyi)),
201 str(int(self.is_ipynb)),
202 str(int(self.magic_trailing_comma)),
203 str(int(self.experimental_string_processing)),
204 str(int(self.preview)),
205 sha256((",".join(sorted(self.python_cell_magics))).encode()).hexdigest(),
207 return ".".join(parts)