Skip to content

Remove/improve some more casts to Any #14828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ def parse_type_comment(
else:
extra_ignore = TYPE_IGNORE_PATTERN.match(type_comment)
if extra_ignore:
# Typeshed has a non-optional return type for group!
tag: str | None = cast(Any, extra_ignore).group(1)
tag: str | None = extra_ignore.group(1)
ignored: list[str] | None = parse_type_ignore_tag(tag)
if ignored is None:
if errors is not None:
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5810,7 +5810,7 @@ def _get_node_for_class_scoped_import(
# mypyc is absolutely convinced that `symbol_node` narrows to a Var in the following,
# when it can also be a FuncBase. Once fixed, `f` in the following can be removed.
# See also https://github.com/mypyc/mypyc/issues/892
f = cast(Any, lambda x: x)
f: Callable[[object], Any] = lambda x: x
if isinstance(f(symbol_node), (Decorator, FuncBase, Var)):
# For imports in class scope, we construct a new node to represent the symbol and
# set its `info` attribute to `self.type`.
Expand Down
18 changes: 13 additions & 5 deletions mypyc/codegen/literals.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from __future__ import annotations

from typing import Any, FrozenSet, List, Tuple, Union, cast
from typing_extensions import Final
from typing import FrozenSet, List, Tuple, Union
from typing_extensions import Final, TypeGuard

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


def _is_literal_value(obj: object) -> TypeGuard[LiteralValue]:
return isinstance(obj, (str, bytes, int, float, complex, tuple, frozenset, type(None)))


# Some literals are singletons and handled specially (None, False and True)
NUM_SINGLETONS: Final = 3

Expand Down Expand Up @@ -55,13 +60,15 @@ def record_literal(self, value: LiteralValue) -> None:
tuple_literals = self.tuple_literals
if value not in tuple_literals:
for item in value:
self.record_literal(cast(Any, item))
assert _is_literal_value(item)
self.record_literal(item)
tuple_literals[value] = len(tuple_literals)
elif isinstance(value, frozenset):
frozenset_literals = self.frozenset_literals
if value not in frozenset_literals:
for item in value:
self.record_literal(cast(Any, item))
assert _is_literal_value(item)
self.record_literal(item)
frozenset_literals[value] = len(frozenset_literals)
else:
assert False, "invalid literal: %r" % value
Expand Down Expand Up @@ -159,7 +166,8 @@ def _encode_collection_values(
value = value_by_index[i]
result.append(str(len(value)))
for item in value:
index = self.literal_index(cast(Any, item))
assert _is_literal_value(item)
index = self.literal_index(item)
result.append(str(index))
return result

Expand Down