]> git.madduck.net Git - etc/vim.git/blob - src/black/mode.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

374c47a42eb0a661e28eb097dccb17e102582a3a
[etc/vim.git] / src / black / mode.py
1 """Data structures configuring Black behavior.
2
3 Mostly around Python language feature support per version and Black configuration
4 chosen by the user.
5 """
6
7 from dataclasses import dataclass, field
8 from enum import Enum
9 from typing import Dict, Set
10
11 from black.const import DEFAULT_LINE_LENGTH
12
13
14 class TargetVersion(Enum):
15     PY27 = 2
16     PY33 = 3
17     PY34 = 4
18     PY35 = 5
19     PY36 = 6
20     PY37 = 7
21     PY38 = 8
22     PY39 = 9
23
24     def is_python2(self) -> bool:
25         return self is TargetVersion.PY27
26
27
28 class Feature(Enum):
29     # All string literals are unicode
30     UNICODE_LITERALS = 1
31     F_STRINGS = 2
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.
37     ASYNC_IDENTIFIERS = 6
38     ASYNC_KEYWORDS = 7
39     ASSIGNMENT_EXPRESSIONS = 8
40     POS_ONLY_ARGUMENTS = 9
41     RELAXED_DECORATORS = 10
42     FORCE_OPTIONAL_PARENTHESES = 50
43
44     # temporary for Python 2 deprecation
45     PRINT_STMT = 200
46     EXEC_STMT = 201
47
48
49 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
50     TargetVersion.PY27: {
51         Feature.ASYNC_IDENTIFIERS,
52         Feature.PRINT_STMT,
53         Feature.EXEC_STMT,
54     },
55     TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
56     TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
57     TargetVersion.PY35: {
58         Feature.UNICODE_LITERALS,
59         Feature.TRAILING_COMMA_IN_CALL,
60         Feature.ASYNC_IDENTIFIERS,
61     },
62     TargetVersion.PY36: {
63         Feature.UNICODE_LITERALS,
64         Feature.F_STRINGS,
65         Feature.NUMERIC_UNDERSCORES,
66         Feature.TRAILING_COMMA_IN_CALL,
67         Feature.TRAILING_COMMA_IN_DEF,
68         Feature.ASYNC_IDENTIFIERS,
69     },
70     TargetVersion.PY37: {
71         Feature.UNICODE_LITERALS,
72         Feature.F_STRINGS,
73         Feature.NUMERIC_UNDERSCORES,
74         Feature.TRAILING_COMMA_IN_CALL,
75         Feature.TRAILING_COMMA_IN_DEF,
76         Feature.ASYNC_KEYWORDS,
77     },
78     TargetVersion.PY38: {
79         Feature.UNICODE_LITERALS,
80         Feature.F_STRINGS,
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,
87     },
88     TargetVersion.PY39: {
89         Feature.UNICODE_LITERALS,
90         Feature.F_STRINGS,
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,
98     },
99 }
100
101
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)
104
105
106 @dataclass
107 class Mode:
108     target_versions: Set[TargetVersion] = field(default_factory=set)
109     line_length: int = DEFAULT_LINE_LENGTH
110     string_normalization: bool = True
111     is_pyi: bool = False
112     is_ipynb: bool = False
113     magic_trailing_comma: bool = True
114     experimental_string_processing: bool = False
115
116     def get_cache_key(self) -> str:
117         if self.target_versions:
118             version_str = ",".join(
119                 str(version.value)
120                 for version in sorted(self.target_versions, key=lambda v: v.value)
121             )
122         else:
123             version_str = "-"
124         parts = [
125             version_str,
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)),
132         ]
133         return ".".join(parts)