]> 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:

3c167569498172daa48560f863059da02fa2ba9b
[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 operator import attrgetter
10 from typing import Dict, Set
11
12 from black.const import DEFAULT_LINE_LENGTH
13
14
15 class TargetVersion(Enum):
16     PY27 = 2
17     PY33 = 3
18     PY34 = 4
19     PY35 = 5
20     PY36 = 6
21     PY37 = 7
22     PY38 = 8
23     PY39 = 9
24     PY310 = 10
25
26     def is_python2(self) -> bool:
27         return self is TargetVersion.PY27
28
29
30 class Feature(Enum):
31     # All string literals are unicode
32     UNICODE_LITERALS = 1
33     F_STRINGS = 2
34     NUMERIC_UNDERSCORES = 3
35     TRAILING_COMMA_IN_CALL = 4
36     TRAILING_COMMA_IN_DEF = 5
37     # The following two feature-flags are mutually exclusive, and exactly one should be
38     # set for every version of python.
39     ASYNC_IDENTIFIERS = 6
40     ASYNC_KEYWORDS = 7
41     ASSIGNMENT_EXPRESSIONS = 8
42     POS_ONLY_ARGUMENTS = 9
43     RELAXED_DECORATORS = 10
44     PATTERN_MATCHING = 11
45     FORCE_OPTIONAL_PARENTHESES = 50
46
47     # temporary for Python 2 deprecation
48     PRINT_STMT = 200
49     EXEC_STMT = 201
50     AUTOMATIC_PARAMETER_UNPACKING = 202
51     COMMA_STYLE_EXCEPT = 203
52     COMMA_STYLE_RAISE = 204
53     LONG_INT_LITERAL = 205
54     OCTAL_INT_LITERAL = 206
55     BACKQUOTE_REPR = 207
56
57
58 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
59     TargetVersion.PY27: {
60         Feature.ASYNC_IDENTIFIERS,
61         Feature.PRINT_STMT,
62         Feature.EXEC_STMT,
63         Feature.AUTOMATIC_PARAMETER_UNPACKING,
64         Feature.COMMA_STYLE_EXCEPT,
65         Feature.COMMA_STYLE_RAISE,
66         Feature.LONG_INT_LITERAL,
67         Feature.OCTAL_INT_LITERAL,
68         Feature.BACKQUOTE_REPR,
69     },
70     TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
71     TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
72     TargetVersion.PY35: {
73         Feature.UNICODE_LITERALS,
74         Feature.TRAILING_COMMA_IN_CALL,
75         Feature.ASYNC_IDENTIFIERS,
76     },
77     TargetVersion.PY36: {
78         Feature.UNICODE_LITERALS,
79         Feature.F_STRINGS,
80         Feature.NUMERIC_UNDERSCORES,
81         Feature.TRAILING_COMMA_IN_CALL,
82         Feature.TRAILING_COMMA_IN_DEF,
83         Feature.ASYNC_IDENTIFIERS,
84     },
85     TargetVersion.PY37: {
86         Feature.UNICODE_LITERALS,
87         Feature.F_STRINGS,
88         Feature.NUMERIC_UNDERSCORES,
89         Feature.TRAILING_COMMA_IN_CALL,
90         Feature.TRAILING_COMMA_IN_DEF,
91         Feature.ASYNC_KEYWORDS,
92     },
93     TargetVersion.PY38: {
94         Feature.UNICODE_LITERALS,
95         Feature.F_STRINGS,
96         Feature.NUMERIC_UNDERSCORES,
97         Feature.TRAILING_COMMA_IN_CALL,
98         Feature.TRAILING_COMMA_IN_DEF,
99         Feature.ASYNC_KEYWORDS,
100         Feature.ASSIGNMENT_EXPRESSIONS,
101         Feature.POS_ONLY_ARGUMENTS,
102     },
103     TargetVersion.PY39: {
104         Feature.UNICODE_LITERALS,
105         Feature.F_STRINGS,
106         Feature.NUMERIC_UNDERSCORES,
107         Feature.TRAILING_COMMA_IN_CALL,
108         Feature.TRAILING_COMMA_IN_DEF,
109         Feature.ASYNC_KEYWORDS,
110         Feature.ASSIGNMENT_EXPRESSIONS,
111         Feature.RELAXED_DECORATORS,
112         Feature.POS_ONLY_ARGUMENTS,
113     },
114     TargetVersion.PY310: {
115         Feature.PATTERN_MATCHING,
116     },
117 }
118
119
120 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
121     return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
122
123
124 @dataclass
125 class Mode:
126     target_versions: Set[TargetVersion] = field(default_factory=set)
127     line_length: int = DEFAULT_LINE_LENGTH
128     string_normalization: bool = True
129     is_pyi: bool = False
130     is_ipynb: bool = False
131     magic_trailing_comma: bool = True
132     experimental_string_processing: bool = False
133
134     def get_cache_key(self) -> str:
135         if self.target_versions:
136             version_str = ",".join(
137                 str(version.value)
138                 for version in sorted(self.target_versions, key=attrgetter("value"))
139             )
140         else:
141             version_str = "-"
142         parts = [
143             version_str,
144             str(self.line_length),
145             str(int(self.string_normalization)),
146             str(int(self.is_pyi)),
147             str(int(self.is_ipynb)),
148             str(int(self.magic_trailing_comma)),
149             str(int(self.experimental_string_processing)),
150         ]
151         return ".".join(parts)