From cb1654555f7484148c0da5e0240636981cf98bee Mon Sep 17 00:00:00 2001 From: David Euresti Date: Fri, 16 Feb 2018 16:14:45 -0800 Subject: [PATCH] Fix crash in attrs_plugin when using a converter --- mypy/plugin.py | 4 ++++ mypy/plugins/attrs.py | 2 +- test-data/unit/check-incremental.test | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mypy/plugin.py b/mypy/plugin.py index 3e1e2bea3011..ac154d441c53 100644 --- a/mypy/plugin.py +++ b/mypy/plugin.py @@ -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. diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index 5ba4cd2c236b..bc84c483d886 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -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) diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index dc94a64402da..691f060e0377 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -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)