Skip to content

Type narrowing ClassVar in subclass prohibits a grandchild to use base type #14702

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

Closed
thejcannon opened this issue Feb 14, 2023 · 1 comment
Closed
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides

Comments

@thejcannon
Copy link
Contributor

Bug Report

I think the reproduction code speaks for itself 😅

To Reproduce

Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=333f013c023a7d031cf8ecfb30143bc3
Code:

from __future__ import annotations
from typing import ClassVar

class Foo:
    x: ClassVar[int | str]

class Bar(Foo):
    x = "s"

class Baz(Bar):
    x = 0  # womp womp

reveal_type(Baz.x)

Expected Behavior

The type didn't get narrowed when inheriting from Bar such that I can still assign x to int.

Note pyright doesn't have this particular affliction.

Actual Behavior

# Incompatible types in assignment (expression has type "int", base class "Bar" defined the type as "str") [assignment]

Your Environment

(See playground link))

@thejcannon thejcannon added the bug mypy got something wrong label Feb 14, 2023
@hauntsaninja
Copy link
Collaborator

Note that pyright isn't narrowing types (since it infers int | str, not int), it's using the context of the parent type to determine the type in the Bar class. This is a missing feature from mypy, but I'd track this in e.g. #10375

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2023
@AlexWaygood AlexWaygood added the topic-inheritance Inheritance and incompatible overrides label Feb 14, 2023
chrisjrn pushed a commit to pantsbuild/pants that referenced this issue Feb 14, 2023
This allows for lazilly-deferred `f`-strings to be used in `Target` and
`Field` help messages, allowing for forward-referencing classes in the
same module.

Due to some [annoyance in
`mypy`](python/mypy#14702), this also adds a
`help_text` helper function that ensures that types aren't narrowed by
subclasses of `Field` or `Target`. This, unfortunately, represents the
bulk of this PR.
seve-martinez pushed a commit to seve-martinez/pants that referenced this issue Feb 20, 2023
This allows for lazilly-deferred `f`-strings to be used in `Target` and
`Field` help messages, allowing for forward-referencing classes in the
same module.

Due to some [annoyance in
`mypy`](python/mypy#14702), this also adds a
`help_text` helper function that ensures that types aren't narrowed by
subclasses of `Field` or `Target`. This, unfortunately, represents the
bulk of this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

3 participants