Skip to content

Optional generic not accepted with --strict-optional #2678

Closed
@FuegoFro

Description

@FuegoFro

When running with --strict-optional, mypy complains about the last statement here. However, this seems like it should work, both because it does work when --strict-optional is left out, and because the revealed type looks like it should match.

from typing import Optional, Type, TypeVar


class BaseThing: pass
class ChildThing(BaseThing): pass


ThingClass = TypeVar("ThingClass", bound=BaseThing)


def get_thing(thing_class: Type[ThingClass]) -> Optional[ThingClass]:
    """ Get the thing with the given class. Stub implementation. """
    return None

thing = None  # type: Optional[ChildThing]

# Fails, complaining about `Type argument 1 of "get_thing" has incompatible value "Optional[ChildThing]"`
thing = get_thing(ChildThing)

# Revealed type is `Union[scratch_1.ChildThing*, builtins.None]`, which seems like it should 
# match Optional[ChildThing].
reveal_type(get_thing(ChildThing))

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions