Skip to content

Commit c91849d

Browse files
authored
Remove/improve some more casts to Any (#14828)
1 parent 456dcbd commit c91849d

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ def parse_type_comment(
351351
else:
352352
extra_ignore = TYPE_IGNORE_PATTERN.match(type_comment)
353353
if extra_ignore:
354-
# Typeshed has a non-optional return type for group!
355-
tag: str | None = cast(Any, extra_ignore).group(1)
354+
tag: str | None = extra_ignore.group(1)
356355
ignored: list[str] | None = parse_type_ignore_tag(tag)
357356
if ignored is None:
358357
if errors is not None:

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5810,7 +5810,7 @@ def _get_node_for_class_scoped_import(
58105810
# mypyc is absolutely convinced that `symbol_node` narrows to a Var in the following,
58115811
# when it can also be a FuncBase. Once fixed, `f` in the following can be removed.
58125812
# See also https://github.com/mypyc/mypyc/issues/892
5813-
f = cast(Any, lambda x: x)
5813+
f: Callable[[object], Any] = lambda x: x
58145814
if isinstance(f(symbol_node), (Decorator, FuncBase, Var)):
58155815
# For imports in class scope, we construct a new node to represent the symbol and
58165816
# set its `info` attribute to `self.type`.

mypyc/codegen/literals.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
from __future__ import annotations
22

3-
from typing import Any, FrozenSet, List, Tuple, Union, cast
4-
from typing_extensions import Final
3+
from typing import FrozenSet, List, Tuple, Union
4+
from typing_extensions import Final, TypeGuard
55

66
# Supported Python literal types. All tuple / frozenset items must have supported
77
# literal types as well, but we can't represent the type precisely.
88
LiteralValue = Union[
99
str, bytes, int, bool, float, complex, Tuple[object, ...], FrozenSet[object], None
1010
]
1111

12+
13+
def _is_literal_value(obj: object) -> TypeGuard[LiteralValue]:
14+
return isinstance(obj, (str, bytes, int, float, complex, tuple, frozenset, type(None)))
15+
16+
1217
# Some literals are singletons and handled specially (None, False and True)
1318
NUM_SINGLETONS: Final = 3
1419

@@ -55,13 +60,15 @@ def record_literal(self, value: LiteralValue) -> None:
5560
tuple_literals = self.tuple_literals
5661
if value not in tuple_literals:
5762
for item in value:
58-
self.record_literal(cast(Any, item))
63+
assert _is_literal_value(item)
64+
self.record_literal(item)
5965
tuple_literals[value] = len(tuple_literals)
6066
elif isinstance(value, frozenset):
6167
frozenset_literals = self.frozenset_literals
6268
if value not in frozenset_literals:
6369
for item in value:
64-
self.record_literal(cast(Any, item))
70+
assert _is_literal_value(item)
71+
self.record_literal(item)
6572
frozenset_literals[value] = len(frozenset_literals)
6673
else:
6774
assert False, "invalid literal: %r" % value
@@ -159,7 +166,8 @@ def _encode_collection_values(
159166
value = value_by_index[i]
160167
result.append(str(len(value)))
161168
for item in value:
162-
index = self.literal_index(cast(Any, item))
169+
assert _is_literal_value(item)
170+
index = self.literal_index(item)
163171
result.append(str(index))
164172
return result
165173

0 commit comments

Comments
 (0)