Skip to content

Commit 8fe3180

Browse files
committed
Fix ##3397
1 parent 3bb14f6 commit 8fe3180

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

tests/test_visitors/test_ast/test_builtins/test_numbers/test_magic_numbers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def function_name(param1, param2: int = {0}):
2929
set_definition = '{{"first", {0}, "other"}}'
3030
tuple_definition = '({0}, )'
3131

32+
literal_type_hint = 'code: Literal[{0}] = {0}'
33+
3234
# Wrong usages:
3335

3436
assignment_binop = 'final = {0} + 1'
@@ -68,6 +70,10 @@ def method(self):
6870
some_dict[{0}]
6971
"""
7072

73+
not_literal_type_hint = """
74+
foo: Bar[{0}] = {0}
75+
"""
76+
7177

7278
@pytest.mark.parametrize(
7379
'code',
@@ -83,6 +89,7 @@ def method(self):
8389
dict_definition_value,
8490
set_definition,
8591
tuple_definition,
92+
literal_type_hint,
8693
],
8794
)
8895
@pytest.mark.parametrize(
@@ -145,6 +152,7 @@ def test_magic_number(
145152
inside_method,
146153
list_index,
147154
dict_key,
155+
not_literal_type_hint
148156
],
149157
)
150158
@pytest.mark.parametrize(
@@ -194,6 +202,7 @@ def test_magic_number_whitelist(
194202
inside_method,
195203
list_index,
196204
dict_key,
205+
not_literal_type_hint
197206
],
198207
)
199208
@pytest.mark.parametrize(
@@ -244,6 +253,7 @@ def test_magic_number_warning(
244253
inside_method,
245254
list_index,
246255
dict_key,
256+
not_literal_type_hint
247257
],
248258
)
249259
@pytest.mark.parametrize(

wemake_python_styleguide/visitors/ast/builtins.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ def visit_Num(self, node: ast.Constant) -> None:
189189

190190
def _check_is_magic(self, node: ast.Constant) -> None:
191191
parent = operators.get_parent_ignoring_unary(node)
192-
if isinstance(parent, self._allowed_parents):
193-
return
194-
195-
if node.value in constants.MAGIC_NUMBERS_WHITELIST:
196-
return
197192

198-
if isinstance(node.value, int) and node.value <= self._non_magic_modulo:
193+
if any((
194+
isinstance(parent, self._allowed_parents),
195+
node.value in constants.MAGIC_NUMBERS_WHITELIST,
196+
isinstance(node.value, int)
197+
and node.value <= self._non_magic_modulo,
198+
self._check_is_number_in_typing_literal(parent),
199+
)):
199200
return
200-
201201
try:
202202
token = self._token_dict[node.lineno, node.col_offset]
203203
except KeyError: # pragma: no cover
@@ -212,6 +212,13 @@ def _check_is_magic(self, node: ast.Constant) -> None:
212212
best_practices.MagicNumberViolation(node, text=real_value),
213213
)
214214

215+
def _check_is_number_in_typing_literal(self, node: ast.AST | None) -> bool:
216+
return (
217+
isinstance(node, ast.Subscript)
218+
and isinstance(node.value, ast.Name)
219+
and node.value.id == 'Literal'
220+
)
221+
215222
def _check_is_approximate_constant(self, node: ast.Constant) -> None:
216223
try:
217224
precision = len(str(node.value).split('.')[1])

0 commit comments

Comments
 (0)