python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.46k stars 2.83k forks source link

Inline staticmethod assignment on subclass fails with `Incompatible types in assignment` #4574

Open momandine opened 6 years ago

momandine commented 6 years ago

A simplification of some old code I am trying to add mypy to:

class Base(object):
    def foo(self):
         # type: () -> None
         a = self.static_foo()

class SubClass(Base):
    static_foo = staticmethod(lambda: 5)

Results in the error: error: Incompatible types in assignment (expression has type "staticmethod", base class "Job" defined the type as "Callable[[], int]")

Oddly, as far as I can tell, static_foo is not actually defined on the base class.

This is using Dropbox's mypy setup, with --strict-optional. I am actually not sure which version we're on, but can look it up and edit this issue.

gvanrossum commented 6 years ago

There must be a little more going on than you show, since if I type that example into mypy:

class Base:
    def foo(self) -> None:
        a = self.static_foo()
class Sub(Base):
    static_foo = staticmethod(lambda: 5)

I get a much simpler error:

_.py:3: error: "Base" has no attribute "static_foo"
momandine commented 6 years ago

Well, this example doesn't error at all, which is closer:

  1 from typing import Any, Dict
  2
  3 class Base(object):
  4     def __init__(self, dikt):
  5         # type: (Dict[Any, Any]) -> None
  6         object.__setattr__(self, '_dict', dikt)
  7
  8     def __getattr__(self, name):
  9         # type: (str) -> Any
 10         return self._dict[name]
 11
 12     def __setattr__(self, name, val):
 13         # type: (str, Any) -> None
 14         if name in self._dict:
 15             self._dict[name] = val
 16         else:
 17             object.__setattr__(self, name, val)
 18
 19     def foo(self):
 20          # type: () -> None
 21          a = self.static_foo()
 22
 23
 24
 25 class SubClass(Base):
 26     static_foo = staticmethod(lambda: 5)
momandine commented 6 years ago

This does repro (though in my actual example, there is no staticmethod declared on the base class (???).

  1 from typing import Any, Dict
  2
  3 class Base(object):
  4     def __init__(self, dikt):
  5         # type: (Dict[Any, Any]) -> None
  6         object.__setattr__(self, '_dict', dikt)
  7
  8     def __getattr__(self, name):
  9         # type: (str) -> Any
 10         return self._dict[name]
 11
 12     def __setattr__(self, name, val):
 13         # type: (str, Any) -> None
 14         if name in self._dict:
 15             self._dict[name] = val
 16         else:
 17             object.__setattr__(self, name, val)
 18
 19     def foo(self):
 20          # type: () -> None
 21          a = self.static_foo()
 22
 23     @staticmethod
 24     def static_foo():
 25         # type: () -> int
 26         return 0
 27
 28
 29 class SubClass(Base):
 30     static_foo = staticmethod(lambda: 5)
 31
 32     def __init__(self, *args, **kwargs):
 33         # type: (*Any, **Any) -> None
 34         super(SubClass, self).__init__(*args, **kwargs)
momandine commented 6 years ago

More minimal repro:

class Base(object):
    def foo(self) -> None:
        a = self.static_foo()

    @staticmethod
    def static_foo() -> int:
        return 0

class SubClass(Base):
    static_foo = staticmethod(lambda: 5)
gvanrossum commented 6 years ago

And to clarify, this gives the following error:

_.py:16: error: Incompatible types in assignment (expression has type "staticmethod", base class "Base" defined the type as "Callable[[], int]")

Now it's very clear that the error is caused by mypy's lack of understanding of how staticmethod() relates to @staticmethod.

As to why it gave this same error for you with the real version of the original code, even though there was no definition of static_foo in the base class there, I'm still not sure. But maybe you don't care and you would just like mypy's understanding of staticmethod() to be improved?

momandine commented 6 years ago

Improving mypy's understanding of inline usage of staticmethod seems tractable/higher priority. Given how clever this code is, someone may have monkey patched something somewhere to the same effect of the minimal example.

AlexWaygood commented 2 years ago

The false-positive error is no longer emitted on mypy 0.930+.

youtux commented 2 years ago

@AlexWaygood this is error is back, at least with mypy 0.950 and 3.7 <= python <= 3.9: https://mypy-play.net/?mypy=0.950&python=3.8&gist=1e25a16047bae31555e210da7797f640

AlexWaygood commented 2 years ago

@AlexWaygood this is error is back, at least with mypy 0.950 and 3.7 <= python <= 3.9: https://mypy-play.net/?mypy=0.950&python=3.8&gist=1e25a16047bae31555e210da7797f640

Hah! This bug was never fixed. The reason I thought it was is because I was testing on 3.10, and the typeshed stub (correctly) states that staticmethod.__call__ was added in 3.10. This means that there's no false positive error on 3.10, but it's not because the mypy bug is fixed, it's because the typeshed stub is different if you're running on 3.10.

AlexWaygood commented 2 years ago

Thanks @youtux :)