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
49 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
51 Feature.ASYNC_IDENTIFIERS,
55 TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
56 TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
58 Feature.UNICODE_LITERALS,
59 Feature.TRAILING_COMMA_IN_CALL,
60 Feature.ASYNC_IDENTIFIERS,
63 Feature.UNICODE_LITERALS,
65 Feature.NUMERIC_UNDERSCORES,
66 Feature.TRAILING_COMMA_IN_CALL,
67 Feature.TRAILING_COMMA_IN_DEF,
68 Feature.ASYNC_IDENTIFIERS,
71 Feature.UNICODE_LITERALS,
73 Feature.NUMERIC_UNDERSCORES,
74 Feature.TRAILING_COMMA_IN_CALL,
75 Feature.TRAILING_COMMA_IN_DEF,
76 Feature.ASYNC_KEYWORDS,
79 Feature.UNICODE_LITERALS,
81 Feature.NUMERIC_UNDERSCORES,
82 Feature.TRAILING_COMMA_IN_CALL,
83 Feature.TRAILING_COMMA_IN_DEF,
84 Feature.ASYNC_KEYWORDS,
85 Feature.ASSIGNMENT_EXPRESSIONS,
86 Feature.POS_ONLY_ARGUMENTS,
89 Feature.UNICODE_LITERALS,
91 Feature.NUMERIC_UNDERSCORES,
92 Feature.TRAILING_COMMA_IN_CALL,
93 Feature.TRAILING_COMMA_IN_DEF,
94 Feature.ASYNC_KEYWORDS,
95 Feature.ASSIGNMENT_EXPRESSIONS,
96 Feature.RELAXED_DECORATORS,
97 Feature.POS_ONLY_ARGUMENTS,
102 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
103 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
108 target_versions: Set[TargetVersion] = field(default_factory=set)
109 line_length: int = DEFAULT_LINE_LENGTH
110 string_normalization: bool = True
112 is_ipynb: bool = False
113 magic_trailing_comma: bool = True
114 experimental_string_processing: bool = False
116 def get_cache_key(self) -> str:
117 if self.target_versions:
118 version_str = ",".join(
120 for version in sorted(self.target_versions, key=lambda v: v.value)
126 str(self.line_length),
127 str(int(self.string_normalization)),
128 str(int(self.is_pyi)),
129 str(int(self.is_ipynb)),
130 str(int(self.magic_trailing_comma)),
131 str(int(self.experimental_string_processing)),
133 return ".".join(parts)