Skip to content

Fix crash in attrs_plugin when using a converter #4583

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
4 changes: 4 additions & 0 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def class_type(self, info: TypeInfo) -> Type:
def lookup_fully_qualified(self, name: str) -> SymbolTableNode:
raise NotImplementedError

@abstractmethod
def lookup_fully_qualified_or_none(self, name: str) -> Optional[SymbolTableNode]:
raise NotImplementedError


# A context for a function hook that infers the return type of a function with
# a special signature.
Expand Down
2 changes: 1 addition & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument:
if self.converter_name:
# When a converter is set the init_type is overriden by the first argument
# of the converter method.
converter = ctx.api.lookup_fully_qualified(self.converter_name)
converter = ctx.api.lookup_fully_qualified_or_none(self.converter_name)
if (converter
and converter.type
and isinstance(converter.type, CallableType)
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3745,6 +3745,28 @@ class C(A, B):
[out1]
[out2]

[case testAttrsIncrementalConverterInSubmodule]
# This test is currently broken (type of x should not be Any). But at least it doesn't crash.
from a.a import A
reveal_type(A)
[file a/__init__.py]
[file a/a.py]
from typing import Optional
def converter(s:Optional[int]) -> int:
...

import attr
@attr.s
class A:
x: int = attr.ib(converter=converter)

[builtins fixtures/list.pyi]
[out1]
main:3: error: Revealed type is 'def (x: Any) -> a.a.A'
[out2]
main:3: error: Revealed type is 'def (x: Any) -> a.a.A'


[case testAttrsIncrementalThreeRuns]
from a import A
A(5)
Expand Down