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
9 from typing import Dict, Set
11 from black.const import DEFAULT_LINE_LENGTH
14 class TargetVersion(Enum):
25 def is_python2(self) -> bool:
26 return self is TargetVersion.PY27
30 # All string literals are unicode
33 NUMERIC_UNDERSCORES = 3
34 TRAILING_COMMA_IN_CALL = 4
35 TRAILING_COMMA_IN_DEF = 5
36 # The following two feature-flags are mutually exclusive, and exactly one should be
37 # set for every version of python.
40 ASSIGNMENT_EXPRESSIONS = 8
41 POS_ONLY_ARGUMENTS = 9
42 RELAXED_DECORATORS = 10
44 FORCE_OPTIONAL_PARENTHESES = 50
46 # temporary for Python 2 deprecation
49 AUTOMATIC_PARAMETER_UNPACKING = 202
50 COMMA_STYLE_EXCEPT = 203
51 COMMA_STYLE_RAISE = 204
52 LONG_INT_LITERAL = 205
53 OCTAL_INT_LITERAL = 206
57 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
59 Feature.ASYNC_IDENTIFIERS,
62 Feature.AUTOMATIC_PARAMETER_UNPACKING,
63 Feature.COMMA_STYLE_EXCEPT,
64 Feature.COMMA_STYLE_RAISE,
65 Feature.LONG_INT_LITERAL,
66 Feature.OCTAL_INT_LITERAL,
67 Feature.BACKQUOTE_REPR,
69 TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
70 TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
72 Feature.UNICODE_LITERALS,
73 Feature.TRAILING_COMMA_IN_CALL,
74 Feature.ASYNC_IDENTIFIERS,
77 Feature.UNICODE_LITERALS,
79 Feature.NUMERIC_UNDERSCORES,
80 Feature.TRAILING_COMMA_IN_CALL,
81 Feature.TRAILING_COMMA_IN_DEF,
82 Feature.ASYNC_IDENTIFIERS,
85 Feature.UNICODE_LITERALS,
87 Feature.NUMERIC_UNDERSCORES,
88 Feature.TRAILING_COMMA_IN_CALL,
89 Feature.TRAILING_COMMA_IN_DEF,
90 Feature.ASYNC_KEYWORDS,
93 Feature.UNICODE_LITERALS,
95 Feature.NUMERIC_UNDERSCORES,
96 Feature.TRAILING_COMMA_IN_CALL,
97 Feature.TRAILING_COMMA_IN_DEF,
98 Feature.ASYNC_KEYWORDS,
99 Feature.ASSIGNMENT_EXPRESSIONS,
100 Feature.POS_ONLY_ARGUMENTS,
102 TargetVersion.PY39: {
103 Feature.UNICODE_LITERALS,
105 Feature.NUMERIC_UNDERSCORES,
106 Feature.TRAILING_COMMA_IN_CALL,
107 Feature.TRAILING_COMMA_IN_DEF,
108 Feature.ASYNC_KEYWORDS,
109 Feature.ASSIGNMENT_EXPRESSIONS,
110 Feature.RELAXED_DECORATORS,
111 Feature.POS_ONLY_ARGUMENTS,
113 TargetVersion.PY310: {
114 Feature.PATTERN_MATCHING,
119 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
120 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
125 target_versions: Set[TargetVersion] = field(default_factory=set)
126 line_length: int = DEFAULT_LINE_LENGTH
127 string_normalization: bool = True
129 is_ipynb: bool = False
130 magic_trailing_comma: bool = True
131 experimental_string_processing: bool = False
133 def get_cache_key(self) -> str:
134 if self.target_versions:
135 version_str = ",".join(
137 for version in sorted(self.target_versions, key=lambda v: v.value)
143 str(self.line_length),
144 str(int(self.string_normalization)),
145 str(int(self.is_pyi)),
146 str(int(self.is_ipynb)),
147 str(int(self.magic_trailing_comma)),
148 str(int(self.experimental_string_processing)),
150 return ".".join(parts)