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
9 from dataclasses import dataclass, field
11 from operator import attrgetter
12 from typing import Dict, Set
14 if sys.version_info < (3, 8):
15 from typing_extensions import Final
17 from typing import Final
19 from black.const import DEFAULT_LINE_LENGTH
22 class TargetVersion(Enum):
33 def is_python2(self) -> bool:
34 return self is TargetVersion.PY27
38 # All string literals are unicode
41 NUMERIC_UNDERSCORES = 3
42 TRAILING_COMMA_IN_CALL = 4
43 TRAILING_COMMA_IN_DEF = 5
44 # The following two feature-flags are mutually exclusive, and exactly one should be
45 # set for every version of python.
48 ASSIGNMENT_EXPRESSIONS = 8
49 POS_ONLY_ARGUMENTS = 9
50 RELAXED_DECORATORS = 10
52 FORCE_OPTIONAL_PARENTHESES = 50
55 FUTURE_ANNOTATIONS = 51
57 # temporary for Python 2 deprecation
60 AUTOMATIC_PARAMETER_UNPACKING = 202
61 COMMA_STYLE_EXCEPT = 203
62 COMMA_STYLE_RAISE = 204
63 LONG_INT_LITERAL = 205
64 OCTAL_INT_LITERAL = 206
68 FUTURE_FLAG_TO_FEATURE: Final = {
69 "annotations": Feature.FUTURE_ANNOTATIONS,
73 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
75 Feature.ASYNC_IDENTIFIERS,
78 Feature.AUTOMATIC_PARAMETER_UNPACKING,
79 Feature.COMMA_STYLE_EXCEPT,
80 Feature.COMMA_STYLE_RAISE,
81 Feature.LONG_INT_LITERAL,
82 Feature.OCTAL_INT_LITERAL,
83 Feature.BACKQUOTE_REPR,
85 TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
86 TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
88 Feature.UNICODE_LITERALS,
89 Feature.TRAILING_COMMA_IN_CALL,
90 Feature.ASYNC_IDENTIFIERS,
93 Feature.UNICODE_LITERALS,
95 Feature.NUMERIC_UNDERSCORES,
96 Feature.TRAILING_COMMA_IN_CALL,
97 Feature.TRAILING_COMMA_IN_DEF,
98 Feature.ASYNC_IDENTIFIERS,
100 TargetVersion.PY37: {
101 Feature.UNICODE_LITERALS,
103 Feature.NUMERIC_UNDERSCORES,
104 Feature.TRAILING_COMMA_IN_CALL,
105 Feature.TRAILING_COMMA_IN_DEF,
106 Feature.ASYNC_KEYWORDS,
107 Feature.FUTURE_ANNOTATIONS,
109 TargetVersion.PY38: {
110 Feature.UNICODE_LITERALS,
112 Feature.NUMERIC_UNDERSCORES,
113 Feature.TRAILING_COMMA_IN_CALL,
114 Feature.TRAILING_COMMA_IN_DEF,
115 Feature.ASYNC_KEYWORDS,
116 Feature.FUTURE_ANNOTATIONS,
117 Feature.ASSIGNMENT_EXPRESSIONS,
118 Feature.POS_ONLY_ARGUMENTS,
120 TargetVersion.PY39: {
121 Feature.UNICODE_LITERALS,
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,
132 TargetVersion.PY310: {
133 Feature.UNICODE_LITERALS,
135 Feature.NUMERIC_UNDERSCORES,
136 Feature.TRAILING_COMMA_IN_CALL,
137 Feature.TRAILING_COMMA_IN_DEF,
138 Feature.ASYNC_KEYWORDS,
139 Feature.FUTURE_ANNOTATIONS,
140 Feature.ASSIGNMENT_EXPRESSIONS,
141 Feature.RELAXED_DECORATORS,
142 Feature.POS_ONLY_ARGUMENTS,
143 Feature.PATTERN_MATCHING,
148 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
149 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
154 target_versions: Set[TargetVersion] = field(default_factory=set)
155 line_length: int = DEFAULT_LINE_LENGTH
156 string_normalization: bool = True
158 is_ipynb: bool = False
159 magic_trailing_comma: bool = True
160 experimental_string_processing: bool = False
162 def get_cache_key(self) -> str:
163 if self.target_versions:
164 version_str = ",".join(
166 for version in sorted(self.target_versions, key=attrgetter("value"))
172 str(self.line_length),
173 str(int(self.string_normalization)),
174 str(int(self.is_pyi)),
175 str(int(self.is_ipynb)),
176 str(int(self.magic_trailing_comma)),
177 str(int(self.experimental_string_processing)),
179 return ".".join(parts)