agronholm / typeguard

Run-time type checker for Python
Other
1.5k stars 112 forks source link

check_type(True, float) == True #448

Closed juliendubost closed 5 months ago

juliendubost commented 5 months ago

Things to check first

Typeguard version

4.2.1

Python version

3.11

What happened?

I noticed a strange behavior of check_type i can't get to explain

check_type(True, int) pass : since bool is a subclass of int it can make sense, isinstance(True, int) == True

But, this should raise TypeCheckError check_type(True, float) pass whereas isinstance(True,float) == False

How can we reproduce the bug?

from typeguard import check_type
check_type(True, float)  # pass
agronholm commented 5 months ago

What is your expectation of the outcome of check_type(1, float)?

juliendubost commented 5 months ago

@agronholm => i would expect a TypeCheckError to be raised since isinstance(1, float) return False.

What do you think ?

agronholm commented 5 months ago

I don't think it should raise TypeCheckError because PEP 484 says that int should pass as a float, and since bool is also an int, check_type(True, float) should also pass.

agronholm commented 5 months ago

And mypy agrees, so I'm closing this.

juliendubost commented 5 months ago

I see, thanks for your reply :+1: