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
45 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
46 TargetVersion.PY27: {Feature.ASYNC_IDENTIFIERS},
47 TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
48 TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
50 Feature.UNICODE_LITERALS,
51 Feature.TRAILING_COMMA_IN_CALL,
52 Feature.ASYNC_IDENTIFIERS,
55 Feature.UNICODE_LITERALS,
57 Feature.NUMERIC_UNDERSCORES,
58 Feature.TRAILING_COMMA_IN_CALL,
59 Feature.TRAILING_COMMA_IN_DEF,
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_KEYWORDS,
71 Feature.UNICODE_LITERALS,
73 Feature.NUMERIC_UNDERSCORES,
74 Feature.TRAILING_COMMA_IN_CALL,
75 Feature.TRAILING_COMMA_IN_DEF,
76 Feature.ASYNC_KEYWORDS,
77 Feature.ASSIGNMENT_EXPRESSIONS,
78 Feature.POS_ONLY_ARGUMENTS,
81 Feature.UNICODE_LITERALS,
83 Feature.NUMERIC_UNDERSCORES,
84 Feature.TRAILING_COMMA_IN_CALL,
85 Feature.TRAILING_COMMA_IN_DEF,
86 Feature.ASYNC_KEYWORDS,
87 Feature.ASSIGNMENT_EXPRESSIONS,
88 Feature.RELAXED_DECORATORS,
89 Feature.POS_ONLY_ARGUMENTS,
94 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
95 return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
100 target_versions: Set[TargetVersion] = field(default_factory=set)
101 line_length: int = DEFAULT_LINE_LENGTH
102 string_normalization: bool = True
104 is_ipynb: bool = False
105 magic_trailing_comma: bool = True
106 experimental_string_processing: bool = False
108 def get_cache_key(self) -> str:
109 if self.target_versions:
110 version_str = ",".join(
112 for version in sorted(self.target_versions, key=lambda v: v.value)
118 str(self.line_length),
119 str(int(self.string_normalization)),
120 str(int(self.is_pyi)),
121 str(int(self.is_ipynb)),
122 str(int(self.magic_trailing_comma)),
123 str(int(self.experimental_string_processing)),
125 return ".".join(parts)