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

Order-dependent errors in class variable type checks #6119

Closed
kaiw opened this issue Dec 30, 2018 · 4 comments
Closed

Order-dependent errors in class variable type checks #6119

kaiw opened this issue Dec 30, 2018 · 4 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@kaiw
Copy link

kaiw commented Dec 30, 2018

Apologies for the apparently-convoluted test case. I've found it to be surprisingly fragile.

class InitiallyUnowned:
    ...


class Bin("Widget"):
    ...


class Dialog("Window"):
    ...


class FontSelectionDialog("Dialog"):
    parent_instance = ...  # type: "Dialog"


class Widget("InitiallyUnowned"):
    parent_instance = ...  # type: "InitiallyUnowned"


class Window("Bin"):
    ...

which, when type-checking the stubs themselves gives:
sample.pyi:16: error: Incompatible types in assignment (expression has type "Dialog", base class "Widget" defined the type as "InitiallyUnowned")

The error appears to be highly order- and hierarchy-dependent.
If Dialog is declared after FontSelectionDialog, the bug goes away.
If Dialog subclasses Bin instead of Window, the bug goes away.
If Window subclasses Widget instead of Bin, the bug goes away.

It's probably worth mentioning that these are auto-generated stubs for GTK+/GObject introspection classes, so just manually adjusting ordering, etc. isn't really a good solution.

What is the behavior/output you expect?

No error.

What are the versions of mypy and Python you are using?

Python 3.6.7, mypy 0.650

Do you see the same issue after installing mypy from Git master?

Yes.

What are the mypy flags you are using? (For example --strict-optional)

No flags. This is just type-checking the stubs themselves.

@ilevkivskyi
Copy link
Member

My guess is this is cause by problems with MRO calculation, so the upcoming semantic analyzer refactoring should fix this.

As a workaround, you can always topologically sort your classes, since Python classes always form a DAG w.r.t. inheritance.

(Also you don't need to use quotes and type comment syntax in stubs)

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels Jan 2, 2019
kaiw added a commit to kaiw/pgi-docgen that referenced this issue Jan 11, 2019
@kaiw
Copy link
Author

kaiw commented Jan 11, 2019

As a workaround, you can always topologically sort your classes, since Python classes always form a DAG w.r.t. inheritance.

Thanks for the suggestion! That does work around the issue just fine.

(Also you don't need to use quotes and type comment syntax in stubs)

I think I could have guessed this for the quoted syntax, but for type comment syntax I assumed that this was Python version dependent. If not, it would be nice to have that noted in the stubs format documentation somewhere. Maybe it's there already and I just missed it.

@genodeftest
Copy link

Shouldn't this issue be gone with python3.7's postponed evaluation of type annotations?

@AlexWaygood
Copy link
Member

The false-positive error is no longer reproducible on mypy 0.720+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants