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

Bump deps in Pipfile.lock (GH-2605)
[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     AUTOMATIC_PARAMETER_UNPACKING = 202
48     COMMA_STYLE_EXCEPT = 203
49     COMMA_STYLE_RAISE = 204
50     LONG_INT_LITERAL = 205
51     OCTAL_INT_LITERAL = 206
52     BACKQUOTE_REPR = 207
53
54
55 VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
56     TargetVersion.PY27: {
57         Feature.ASYNC_IDENTIFIERS,
58         Feature.PRINT_STMT,
59         Feature.EXEC_STMT,
60         Feature.AUTOMATIC_PARAMETER_UNPACKING,
61         Feature.COMMA_STYLE_EXCEPT,
62         Feature.COMMA_STYLE_RAISE,
63         Feature.LONG_INT_LITERAL,
64         Feature.OCTAL_INT_LITERAL,
65         Feature.BACKQUOTE_REPR,
66     },
67     TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
68     TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
69     TargetVersion.PY35: {
70         Feature.UNICODE_LITERALS,
71         Feature.TRAILING_COMMA_IN_CALL,
72         Feature.ASYNC_IDENTIFIERS,
73     },
74     TargetVersion.PY36: {
75         Feature.UNICODE_LITERALS,
76         Feature.F_STRINGS,
77         Feature.NUMERIC_UNDERSCORES,
78         Feature.TRAILING_COMMA_IN_CALL,
79         Feature.TRAILING_COMMA_IN_DEF,
80         Feature.ASYNC_IDENTIFIERS,
81     },
82     TargetVersion.PY37: {
83         Feature.UNICODE_LITERALS,
84         Feature.F_STRINGS,
85         Feature.NUMERIC_UNDERSCORES,
86         Feature.TRAILING_COMMA_IN_CALL,
87         Feature.TRAILING_COMMA_IN_DEF,
88         Feature.ASYNC_KEYWORDS,
89     },
90     TargetVersion.PY38: {
91         Feature.UNICODE_LITERALS,
92         Feature.F_STRINGS,
93         Feature.NUMERIC_UNDERSCORES,
94         Feature.TRAILING_COMMA_IN_CALL,
95         Feature.TRAILING_COMMA_IN_DEF,
96         Feature.ASYNC_KEYWORDS,
97         Feature.ASSIGNMENT_EXPRESSIONS,
98         Feature.POS_ONLY_ARGUMENTS,
99     },
100     TargetVersion.PY39: {
101         Feature.UNICODE_LITERALS,
102         Feature.F_STRINGS,
103         Feature.NUMERIC_UNDERSCORES,
104         Feature.TRAILING_COMMA_IN_CALL,
105         Feature.TRAILING_COMMA_IN_DEF,
106         Feature.ASYNC_KEYWORDS,
107         Feature.ASSIGNMENT_EXPRESSIONS,
108         Feature.RELAXED_DECORATORS,
109         Feature.POS_ONLY_ARGUMENTS,
110     },
111 }
112
113
114 def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
115     return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
116
117
118 @dataclass
119 class Mode:
120     target_versions: Set[TargetVersion] = field(default_factory=set)
121     line_length: int = DEFAULT_LINE_LENGTH
122     string_normalization: bool = True
123     is_pyi: bool = False
124     is_ipynb: bool = False
125     magic_trailing_comma: bool = True
126     experimental_string_processing: bool = False
127
128     def get_cache_key(self) -> str:
129         if self.target_versions:
130             version_str = ",".join(
131                 str(version.value)
132                 for version in sorted(self.target_versions, key=lambda v: v.value)
133             )
134         else:
135             version_str = "-"
136         parts = [
137             version_str,
138             str(self.line_length),
139             str(int(self.string_normalization)),
140             str(int(self.is_pyi)),
141             str(int(self.is_ipynb)),
142             str(int(self.magic_trailing_comma)),
143             str(int(self.experimental_string_processing)),
144         ]
145         return ".".join(parts)