Skip to content

mypy infers conditional expression of Dict[str, object] to be object instead of union #7835

Closed
@scascketta

Description

@scascketta
  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private.
from typing import Dict
from dataclasses import dataclass


@dataclass
class Foo:
    bar: bool


@dataclass
class Bar:
    baz: bool


foo_registry: Dict[str, Foo] = {}
bar_registry: Dict[str, Bar] = {}

use_foo = True

registry = foo_registry if use_foo else bar_registry
registry.get('asdfsdf')
  • What is the actual behavior/output?

On the last line, mypy infers that registry has the type object

error: "object" has no attribute "get"
  • What is the behavior/output you expect?

mypy infers that the type of registry to be Union[Dict[str, Foo], Dict[str, Bar]] but it looks like this behavior may have been undone by #5095 ?

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

mypy 0.740 and Python 3.7.2

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

I ran into other issues doing this, but I am on the latest release.

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

I'm using --strict --warn-unreachable though this occurs with no flags at all.

Thanks for reading.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions