Skip to content

issubclass doesn't narrow down types #10978

Description

@Illviljan

Bug Report

issubclass appears to not narrow down possible types.

To Reproduce

fromtypingimportAny, Mapping, MutableMappingdeftest_mutablemapping(obj: Mapping[Any, Any]) ->MutableMapping[Any, Any]:
ifissubclass(type(obj), MutableMapping):
returnobj# error: Incompatible types in assignment (expression has type "Mapping[Any, Any]", variable has type "MutableMapping[Any, Any]")else:
returndict(obj)

Expected Behavior

To narrow down in similar fashion to isinstance:

fromtypingimportAny, Mapping, MutableMappingdeftest_mutablemapping2(obj: Mapping[Any, Any]) ->MutableMapping[Any, Any]:
ifisinstance(obj, MutableMapping):
returnobjelse:
returndict(obj)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: default
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.8.8
  • Operating system and version: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions