Skip to content

Fix crash on unimported Any with Required/NotRequired #17609

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
Overloaded,
PartialType,
ProperType,
RequiredType,
TupleType,
Type,
TypeAliasType,
Expand Down Expand Up @@ -2945,7 +2946,11 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
"A type on this line", AnyType(TypeOfAny.special_form), s
)
else:
self.msg.unimported_type_becomes_any("Type of variable", s.type, s)
self.msg.unimported_type_becomes_any(
"Type of variable",
s.type.item if isinstance(s.type, RequiredType) else s.type,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to use some existing function to covert RequiredType to ProperType? Like get_proper_type? Or maybe something similar. Right now it feels too specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_proper_type is already being called—that’s where it’s crashing. It doesn’t convert RequiredType. Should it? That seems like a much bigger change though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about it :)
cc @JukkaL and @ilevkivskyi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is not a fix, but a horrible dirty hack. I am actually going to revert another recent crash "fix" by @andersk and replace it with a proper one when I will have time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to have offended you!

s,
)
check_for_explicit_any(s.type, self.options, self.is_typeshed_stub, self.msg, context=s)

if len(s.lvalues) > 1:
Expand Down
3 changes: 2 additions & 1 deletion mypy/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
CallableType,
FunctionLike,
Instance,
RequiredType,
TupleType,
Type,
TypeOfAny,
Expand Down Expand Up @@ -205,7 +206,7 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
if o.type:
# If there is an explicit type, don't visit the l.h.s. as an expression
# to avoid double-counting and mishandling special forms.
self.type(o.type)
self.type(o.type.item if isinstance(o.type, RequiredType) else o.type)
o.rvalue.accept(self)
return
elif self.inferred and not self.all_nodes:
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,14 @@ class ForceDeferredEval: pass
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictRequiredUnimportedAny]
# flags: --disallow-any-unimported
from typing import NotRequired, TypedDict
from nonexistent import Foo # type: ignore[import-not-found]
class Bar(TypedDict):
foo: NotRequired[Foo] # E: Type of variable becomes "Any" due to an unfollowed import
[typing fixtures/typing-typeddict.pyi]

-- Required[]

[case testDoesRecognizeRequiredInTypedDictWithClass]
Expand Down
Loading