Skip to content

typing_extension.Self infers the wrong type from __new__ #4668

Closed

Description

@Avasam

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)

Activity

  1. erictraut commented on Feb 21, 2023

    @erictraut
    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?

  2. Avasam commented on Feb 21, 2023

    @Avasam
    Author

    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" ?

  3. erictraut commented on Feb 21, 2023

    @erictraut
    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.

  4. added
    needs investigationRequires additional investigation to determine course of action
    and removed
    questionFurther information is requested
    on Feb 21, 2023
  5. erictraut commented on Feb 22, 2023

    @erictraut
    Collaborator

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

  6. added
    bugSomething isn't working
    addressed in next versionIssue is fixed and will appear in next published version
    and removed
    needs investigationRequires additional investigation to determine course of action
    on Feb 22, 2023
  7. erictraut commented on Feb 22, 2023

    @erictraut
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions