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

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