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):
24 def is_python2(self) -> bool:
25 return self is TargetVersion.PY27
29 # All string literals are unicode
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
42 FORCE_OPTIONAL_PARENTHESES = 50
44 # temporary for Python 2 deprecation
47 AUTOMATIC_PARAMETER_UNPACKING = 202
48 COMMA_STYLE_EXCEPT = 203
49 COMMA_STYLE_RAISE = 204
50 LONG_INT_LITERAL = 205
51 OCTAL_INT_LITERAL = 206
55 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
57 Feature.ASYNC_IDENTIFIERS,
60 Feature.AUTOMATIC_PARAMETER_UNPACKING,
61 Feature.COMMA_STYLE_EXCEPT,
62 Feature.COMMA_STYLE_RAISE,
63 Feature.LONG_INT_LITERAL,
64 Feature.OCTAL_INT_LITERAL,
65 Feature.BACKQUOTE_REPR,
67 TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
68 TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
70 Feature.UNICODE_LITERALS,
71 Feature.TRAILING_COMMA_IN_CALL,
72 Feature.ASYNC_IDENTIFIERS,
75 Feature.UNICODE_LITERALS,
77 Feature.NUMERIC_UNDERSCORES,
78 Feature.TRAILING_COMMA_IN_CALL,
79 Feature.TRAILING_COMMA_IN_DEF,
80 Feature.ASYNC_IDENTIFIERS,
83 Feature.UNICODE_LITERALS,
85 Feature.NUMERIC_UNDERSCORES,
86 Feature.TRAILING_COMMA_IN_CALL,
87 Feature.TRAILING_COMMA_IN_DEF,
88 Feature.ASYNC_KEYWORDS,
91 Feature.UNICODE_LITERALS,
93 Feature.NUMERIC_UNDERSCORES,
94 Feature.TRAILING_COMMA_IN_CALL,
95 Feature.TRAILING_COMMA_IN_DEF,
96 Feature.ASYNC_KEYWORDS,
97 Feature.ASSIGNMENT_EXPRESSIONS,
98 Feature.POS_ONLY_ARGUMENTS,
100 TargetVersion.PY39: {
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.ASSIGNMENT_EXPRESSIONS,
108 Feature.RELAXED_DECORATORS,
109 Feature.POS_ONLY_ARGUMENTS,
114 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
115 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
120 target_versions: Set[TargetVersion] = field(default_factory=set)
121 line_length: int = DEFAULT_LINE_LENGTH
122 string_normalization: bool = True
124 is_ipynb: bool = False
125 magic_trailing_comma: bool = True
126 experimental_string_processing: bool = False
128 def get_cache_key(self) -> str:
129 if self.target_versions:
130 version_str = ",".join(
132 for version in sorted(self.target_versions, key=lambda v: v.value)
138 str(self.line_length),
139 str(int(self.string_normalization)),
140 str(int(self.is_pyi)),
141 str(int(self.is_ipynb)),
142 str(int(self.magic_trailing_comma)),
143 str(int(self.experimental_string_processing)),
145 return ".".join(parts)