typing icon indicating copy to clipboard operation
typing copied to clipboard

Type checking for `property.{fget,fset,fdel}`

Open jpy-git opened this issue 3 years ago • 4 comments

When trying to fix an issue in typeshed we found that both mypy and pyright raised incorrect errors that were also inconsistent with one another so I'm creating this issue to facilitate further discussion on resolving this.

class A:
    @property
    def foo(self):
        return 'a'

A.foo.fget(A())

mypy: "Callable[[A], Any]" has no attribute "fget" pyright: Expected 0 positional arguments

Initial PR attempt and relevant discussions can be found here

jpy-git avatar Mar 21 '22 23:03 jpy-git

I'd actually been thinking about this recently, I think the best way to solve this moving forwards is for mypy to implement pyright's current behaviour (which is the correct one). Then to make FunctionType and MethodType generic as then users could actually opt-in to the descriptor behaviour if they wanted. The only issue with this is that currently this isn't do-able in the current type system (as far as I'm aware).

See https://github.com/python/typeshed/issues/6347

Gobot1234 avatar Mar 22 '22 00:03 Gobot1234

It looks like both mypy and pyright are currently handling this case incorrectly. The signature of fget isn't documented (as far as I can tell), and I made an incorrect assumption when implementing it in pyright. The problem will be fixed in the next release. Same goes for fset and fdel.

erictraut avatar Mar 22 '22 14:03 erictraut

I don't think the property issue here needs broader discussion; that can be addressed in typeshed or within type checkers. But there's a behavior discrepancy that needs to be addressed; I opened #1113 for that.

JelleZijlstra avatar Mar 22 '22 15:03 JelleZijlstra

I ran into this issue as well but it seems to only happen for properties with an additional decorator @abstractmethod. Edit: Nevermind, I am not sure why it only raises for some properties and not others. I don't see a pattern yet.

antazoey avatar Oct 31 '22 14:10 antazoey