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

typing_extension.Self infers the wrong type from __new__ #4668

Closed
Avasam opened this issue Feb 20, 2023 · 5 comments
Closed

typing_extension.Self infers the wrong type from __new__ #4668

Avasam opened this issue Feb 20, 2023 · 5 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Avasam
Copy link

Avasam commented Feb 20, 2023

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

Describe the bug
When using __new__ directly (example below), the return type is the class itself instead of inferring from the first classmethod param (cls)

To Reproduce

import enum
from typing_extensions import Self, reveal_type


class ActionLevel(enum.IntEnum):
    def __new__(cls, value: int, doc: str) -> Self:
        member = int.__new__(cls, value)
        reveal_type(member)
        member._value_ = value
        member.__doc__ = doc
        return member

Expected behavior
member should be ActionLevel not int

Screenshots or Code
image

VS Code extension or command-line
Both.
npx pyright@1.1.294 .\scripts\stubsabot.py

Additional context
This was first found here: python/typeshed#9694 (comment)

@erictraut
Copy link
Collaborator

I'm not able to repro the problem you're seeing. Here's the behavior I see:

image

Can you confirm that when you paste the above code into a new editor window in a new project with default pyright settings that you get an error?

Perhaps you have an old version of typing_extensions or typing_extensions-stubs installed in your environment?

@erictraut erictraut added the question Further information is requested label Feb 21, 2023
@Avasam
Copy link
Author

Avasam commented Feb 21, 2023

typing_extensions is 4.5.0
I narrowed down what in the configs causes it: setting "typeshedPath". In this case, my minimal pyrightconfig.json is as such:

{
    "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json",
    "typeshedPath": ".",
}

I can confirm I have no typing_extensions-stubs in my env:
image

Note this happens with typing too:
npx pyright@1.1.294 .\scripts\stubsabot.py --pythonversion=3.11

pyright 1.1.294
E:\Users\Avasam\Documents\Git\typeshed\scripts\stubsabot.py
  E:\Users\Avasam\Documents\Git\typeshed\scripts\stubsabot.py:8:21 - information: Type of "member" is "int"
  E:\Users\Avasam\Documents\Git\typeshed\scripts\stubsabot.py:9:16 - error: Cannot assign member "_value_" for type "int"
    Member "_value_" is unknown (reportGeneralTypeIssues)
  E:\Users\Avasam\Documents\Git\typeshed\scripts\stubsabot.py:11:16 - error: Expression of type "int" cannot be assigned to return type "Self@ActionLevel"
    "int" is incompatible with "ActionLevel" (reportGeneralTypeIssues)
2 errors, 0 warnings, 1 information

Is this something that can be fixed on typeshed's side? Or is it due to some special casing done with "typeshedPath" ?

@erictraut
Copy link
Collaborator

Ah, you're using a version of typeshed that is newer than what is bundled with pyright 1.1.294. That's an important detail! I'm able to repro the problem now, so I'll investigate further.

@erictraut erictraut added needs investigation Requires additional investigation to determine course of action and removed question Further information is requested labels Feb 21, 2023
erictraut pushed a commit that referenced this issue Feb 22, 2023
… is called and is bound to a class that is different from the `cls` argument passed to it. This addresses #4668.
@erictraut
Copy link
Collaborator

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

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version and removed needs investigation Requires additional investigation to determine course of action labels Feb 22, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.295, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants