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

Subclass class variables not checked against superclass type hints #1726

Closed
jli opened this issue Aug 25, 2021 · 4 comments
Closed

Subclass class variables not checked against superclass type hints #1726

jli opened this issue Aug 25, 2021 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@jli
Copy link

jli commented Aug 25, 2021

Environment data

  • Language Server version: 2021.8.2 (pyright f462f5bb)
  • OS and version: Mac Big Sur 11.5.2
  • Python version: Python 3.9.4, mypy 0.812

Expected v. actual behaviour

When a subclass sets a class variable that doesn't match the superclass's type hint, pylance should warn about it.

Pylance also infers the wrong type for subclass instance references to the variable.

Code Snippet / Additional information

class Experiment:
    train_epochs: int

    def __init__(self, train_epochs: int):
        self.train_epochs = train_epochs

class ExperimentWithPresets(Experiment):
    # different type than the superclass.
    # hovering over this, Pylance says `str`, which is correct
    train_epochs = "123"

    def __init__(self):
        pass

exp = Experiment(100)
print(f'base Experiment: {exp.train_epochs=}, {type(exp.train_epochs)=}')

# when hovering over exp2.train_epochs, Pylance says `int`, which is wrong
exp2 = ExperimentWithPresets()
print(f'ExperimentWithPresets: {exp2.train_epochs=}, {type(exp2.train_epochs)=}')

Output from python:

% python subclass_fields_type_mismatch.py
base Experiment: exp.train_epochs=100, type(exp.train_epochs)=<class 'int'>
ExperimentWithPresets: exp2.train_epochs='123', type(exp2.train_epochs)=<class 'str'>

Output from mypy:

% mypy subclass_fields_type_mismatch.py
subclass_fields_type_mismatch.py:10: error: Incompatible types in assignment (expression has type "str", base class "Experiment" defined the type as "int")
Found 1 error in 1 file (checked 1 source file)
@erictraut
Copy link
Contributor

erictraut commented Aug 25, 2021

Thanks for reporting this. I agree this is an omission in type checking. Pylance does check the base class type for instance variable assignments, but it doesn't currently do so for class variable assignments in subclasses.

@erictraut
Copy link
Contributor

This will be addressed in the next release.

@erictraut erictraut added enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Aug 28, 2021
@jli
Copy link
Author

jli commented Aug 28, 2021

🙏 thanks so much, really appreciate your responsiveness!

@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#202190-1-september-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 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

3 participants