Skip to content

Property return type not inferred when using field_name = property(func) #13745

@jonathanslenders

Description

@jonathanslenders

Bug Report

When using the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)

To Reproduce

from __future__ import annotations
from typing import Any

def func1(self: Any) -> int:
    return 3

def func2(self: object) -> int:
    return 3

def func3(self: Test) -> int:
    return 3

class Test:
    def func4(self) -> int:
        return 3

    test1 = property(func1)
    test2 = property(func2)
    test3 = property(func3)
    test4 = property(func4)

reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)

# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file

Expected Behavior

I'd expect mypy to understand the property return types.

It also seems that there is no way around this.
The following gives "Incompatible types in assignment":

class Test:
    test4: int = property(func4)

and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)

Actual Behavior

We're getting Any instead.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions