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

Pin setuptools-scm build time dependency (#2457)
[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
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},
49     TargetVersion.PY35: {
50         Feature.UNICODE_LITERALS,
51         Feature.TRAILING_COMMA_IN_CALL,
52         Feature.ASYNC_IDENTIFIERS,
53     },
54     TargetVersion.PY36: {
55         Feature.UNICODE_LITERALS,
56         Feature.F_STRINGS,
57         Feature.NUMERIC_UNDERSCORES,
58         Feature.TRAILING_COMMA_IN_CALL,
59         Feature.TRAILING_COMMA_IN_DEF,
60         Feature.ASYNC_IDENTIFIERS,
61     },
62     TargetVersion.PY37: {
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_KEYWORDS,
69     },
70     TargetVersion.PY38: {
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         Feature.ASSIGNMENT_EXPRESSIONS,
78         Feature.POS_ONLY_ARGUMENTS,
79     },
80     TargetVersion.PY39: {
81         Feature.UNICODE_LITERALS,
82         Feature.F_STRINGS,
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,
90     },
91 }
92
93
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)
96
97
98 @dataclass
99 class Mode:
100     target_versions: Set[TargetVersion] = field(default_factory=set)
101     line_length: int = DEFAULT_LINE_LENGTH
102     string_normalization: bool = True
103     is_pyi: bool = False
104     is_ipynb: bool = False
105     magic_trailing_comma: bool = True
106     experimental_string_processing: bool = False
107
108     def get_cache_key(self) -> str:
109         if self.target_versions:
110             version_str = ",".join(
111                 str(version.value)
112                 for version in sorted(self.target_versions, key=lambda v: v.value)
113             )
114         else:
115             version_str = "-"
116         parts = [
117             version_str,
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)),
124         ]
125         return ".".join(parts)