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

__getattr__ needs to be ignored during operator availability checks #1252

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

Comments

@Azureblade3808
Copy link

Azureblade3808 commented May 8, 2021

Environment data

  • Language Server version: 2021.5.1
  • OS and version: Windows 7
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.8 Anaconda

Sample

class C:
    def __getattr__(self, name: str, /):
        if name == "__add__":
            return lambda _: 0

a = C()
a.__add__  # Implied type: (_: Unknown) -> Literal[0] | None

b = a + 0  # Implied type: Literal[0]

Expected behavior

The last line should be forbidden.

The internal logic of built-in operators / wrapper methods (for example: len(...)) calls the method defined on the class but not the instance, so __getattr__ will not work.

If you run the sample, you will probably get

TypeError: unsupported operand type(s) for +: 'C' and 'int'

on the last line.

Actual behavior

No errors reported.

@erictraut
Copy link
Contributor

Moving this issue to pyright, since it's a core type checker issue.

@erictraut
Copy link
Contributor

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

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed waiting for upstream Waiting for upstream to release a fix labels May 14, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.5.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202153-19-may-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