Skip to content

Incorrect type narrowing for if type(x) == bool #2460

@AlexWaygood

Description

@AlexWaygood

Summary

def f(x: object):
    if type(x) == bool:
        reveal_type(x)  # revealed: ~bool
    else:
        reveal_type(x)  # revealed: ~bool

We shouldn't be doing any narrowing at all here, let alone narrowing the type to ~bool. We should narrow for constructs like if type(x) is bool or if type(x) is not bool, but it's unsafe to apply this kind of narrowing for other operators like == or != (the type of x might have a custom metaclass that allows it to compare equal to bool even though it is not bool)

Version

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingnarrowingrelated to flow-sensitive type narrowing

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions