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

mypy should support optional tagged unions #8925

Closed
chdsbd opened this issue May 30, 2020 · 1 comment · Fixed by #11207
Closed

mypy should support optional tagged unions #8925

chdsbd opened this issue May 30, 2020 · 1 comment · Fixed by #11207
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-strict-optional topic-typed-dict

Comments

@chdsbd
Copy link

chdsbd commented May 30, 2020

Report Type: Bug

Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050

from typing import Literal, TypedDict, Union

class NewJob(TypedDict):
    event_type: Literal["new-job"]
    job_description: str

class CancelJob(TypedDict):
    event_type: Literal["cancel-job"]
    job_id: int

request: Union[NewJob, CancelJob, None]

# works
if request is not None:
    if request["event_type"] == "new-job":
        print("Starting new job", request["job_description"])

# fails with `main.py:20: error: TypedDict "CancelJob" has no key 'job_description'`
if request is not None and request["event_type"] == "new-job":
    print("Starting new job", request["job_description"])
  • What is the actual behavior/output?
    Mypy does not discriminate TypedDicts when None is part of the Union.
  • What is the behavior/output you expect?
    Mypy narrows a Union of TypedDict and None like it does for unions of TypedDicts.
  • What are the versions of mypy and Python you are using?
    mypy 0.770
    Python 3.7.6
  • Do you see the same issue after installing mypy from Git master?
    I have not tried master.

This is a small issue because there is an easy workaround of checking for None before checking the tag.

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-strict-optional topic-typed-dict labels Jun 8, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 8, 2020

This looks like a bug.

bluetech added a commit to bluetech/mypy that referenced this issue Sep 27, 2021
…pressions

Fixes python#8925.

Consider the following narrowing (more realistically with TypedDicts),
and the current outcome:

```py
class A:
    tag: Literal["A"]

class B:
    tag: Literal["B"]

abo: A | B | None
if abo is not None and abo.tag == "A":
    reveal_type(abo.tag)  # Type is Literal["A"]
    reveal_type(abo)  # Type is A | B
```

The RHS of the comparison correctly takes into account the LHS, and the
`abo.tag` expression is correctly narrowed based on it, but this does
not propagate upward to then narrow `abo` to `A`.

The problem is that `and`/`or/`not`/assignment expressions recurse using
the `find_isinstance_check_helper` function, which omits the
`propagate_up_typemap_info` calls that its parent function
`find_isinstance_check` performs.

Fix this by replacing these recursive `find_isinstance_check_helper`
calls with `find_isinstance_check`. This might not always be necessary,
but I do not think that always propagating upwards should do any harm,
anyways.
bluetech added a commit to bluetech/mypy that referenced this issue Sep 27, 2021
…pressions

Fixes python#8925.

Consider the following narrowing (more realistically with TypedDicts),
and the current outcome:

```py
class A:
    tag: Literal["A"]

class B:
    tag: Literal["B"]

abo: A | B | None
if abo is not None and abo.tag == "A":
    reveal_type(abo.tag)  # Type is Literal["A"]
    reveal_type(abo)  # Type is A | B
```

The RHS of the comparison correctly takes into account the LHS, and the
`abo.tag` expression is correctly narrowed based on it, but this does
not propagate upward to then narrow `abo` to `A`.

The problem is that `and`/`or/`not`/assignment expressions recurse using
the `find_isinstance_check_helper` function, which omits the
`propagate_up_typemap_info` calls that its parent function
`find_isinstance_check` performs.

Fix this by replacing these recursive `find_isinstance_check_helper`
calls with `find_isinstance_check`. This might not always be necessary,
but I do not think that always propagating upwards should do any harm,
anyways.
JukkaL pushed a commit that referenced this issue Oct 1, 2021
…pressions (#11207)

Fixes #8925.

Consider the following narrowing (more realistically with TypedDicts),
and the current outcome:

```py
class A:
    tag: Literal["A"]

class B:
    tag: Literal["B"]

abo: A | B | None
if abo is not None and abo.tag == "A":
    reveal_type(abo.tag)  # Type is Literal["A"]
    reveal_type(abo)  # Type is A | B
```

The RHS of the comparison correctly takes into account the LHS, and the
`abo.tag` expression is correctly narrowed based on it, but this does
not propagate upward to then narrow `abo` to `A`.

The problem is that `and`/`or/`not`/assignment expressions recurse using
the `find_isinstance_check_helper` function, which omits the
`propagate_up_typemap_info` calls that its parent function
`find_isinstance_check` performs.

Fix this by replacing these recursive `find_isinstance_check_helper`
calls with `find_isinstance_check`. This might not always be necessary,
but I do not think that always propagating upwards should do any harm,
anyways.
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 topic-strict-optional topic-typed-dict
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants