Skip to content
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

Generic types in annotations of __getattr__ in metaclasses are not deduced correctly #1481

Closed
dedebenui opened this issue Jun 22, 2021 · 2 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@dedebenui
Copy link

dedebenui commented Jun 22, 2021

Environment data

  • Language Server version: 2021.6.2
  • OS and version: darwin x64
  • Python version (and distribution if applicable, e.g. Anaconda): Python 3.9.5 (Anaconda)
  • python.analysis.indexing: undefined
  • python.analysis.typeCheckingMode: off

Expected behaviour

Pylance should determine the type of all 4 outputs to be int

Actual behaviour

Pylance determines that the type of output3 is Type[SomeFunctions] no matter the type of the argument to SomeFunctions.other_function.

Code Snippet / Additional information

Capture d’écran 2021-06-22 à 11 09 56

Capture d’écran 2021-06-22 à 11 11 08

from typing import Callable, TypeVar

T = TypeVar("T")

class FunctionCollection(type):
    def __getattr__(cls, key) -> Callable[[T], T]:
        return lambda x: x

    def function(cls, arg: T) -> T:
        return arg

    def factory(cls) -> Callable[[T], T]:
        return lambda x: x

class SomeFunctions(metaclass=FunctionCollection):
    pass


output1 = SomeFunctions.function(3)
output2 = SomeFunctions.factory()(3)
output3 = SomeFunctions.other_function(3)
output4 = SomeFunctions.__getattr__("other_function")(3)
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

The problem is that the type evaluator assumed that the function returned by the metaclass __getattr__ needed to be bound to the function SomeFunctions. It therefore assumed that the first parameter (of type T) was a self parameter. The Python interpreter appears to know that the return result from __getattr__ should not be treated like other class or instance methods and therefore does not need to be bound by the caller's class, nor does it receive a self argument.

@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed in backlog labels Jun 25, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202170-6-july-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants