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

Weird error message when setting class variable of the wrong type (not in an instance) #6106

Closed
svenzorgdoc opened this issue Oct 9, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@svenzorgdoc
Copy link

svenzorgdoc commented Oct 9, 2023

Describe the bug
There is something weird with Pyright's error message when trying to set an unannotated class variable on the class itself when using an incorrect type.

Code or Screenshots

from typing import ClassVar


class Test:
    number = 1
    text = "hello"
    number_as_classvar: ClassVar[int] = 1
    text_as_classvar: ClassVar[str] = "hello"


Test.number = 4  # no error, correct
Test.text = 4  # WEIRD ERROR

Test.number_as_classvar = 4  # no error, correct
Test.text_as_classvar = 4  # correct error

Test.fdjkdfs = 3  # correct error

The result is:

pyright 1.1.300
/tmp/huh.py
  /tmp/huh.py:12:6 - error: Cannot assign member "text" for type "Type[Test]"
    Member "text" is unknown (reportGeneralTypeIssues)
  /tmp/huh.py:15:25 - error: Cannot assign member "text_as_classvar" for type "Type[Test]"
    "Literal[4]" is incompatible with "str" (reportGeneralTypeIssues)
  /tmp/huh.py:17:6 - error: Cannot assign member "fdjkdfs" for type "Type[Test]"
    Member "fdjkdfs" is unknown (reportGeneralTypeIssues)
3 errors, 0 warnings, 0 informations 

The error on /tmp/huh.py:12:6 is unexpected.

VS Code extension or command-line
Tested using pyright command line tool version 1.1.300.

@svenzorgdoc svenzorgdoc added the bug Something isn't working label Oct 9, 2023
erictraut pushed a commit that referenced this issue Oct 9, 2023
…lue with an incompatible type to a class variable that has no explicit type declaration. This addresses #6106.
erictraut added a commit that referenced this issue Oct 9, 2023
…lue with an incompatible type to a class variable that has no explicit type declaration. This addresses #6106. (#6107)

Co-authored-by: Eric Traut <erictr@microsoft.com>
@erictraut
Copy link
Collaborator

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

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 9, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.331, 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