KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
https://kotlinisland.github.io/basedmypy/
Other
144 stars 4 forks source link

Covariant `Mapping`s key type? #803

Closed LeeeeT closed 2 weeks ago

LeeeeT commented 2 weeks ago

Describe the problem

Why is Mappings key type made covariant? This is already discussed and showed to be unsafe in https://github.com/python/typing/pull/273. This contradicts type theory.

Gist to reproduce

No response

Severity

annoying but i can live with it

Your Environment

No response

KotlinIsland commented 2 weeks ago

see #62

it's covariant because it's safe. the only case in which it would be unsafe is when someone writes a __getitem__/get, disables the error that mypy produces, and then additionally misuses the input value in an unsafe way:

playground

thanks for looking into basedmypy, if you have any questions i'm happy to discuss. we also have a discord if your interested

LeeeeT commented 2 weeks ago

Do I understand this correctly? Every time someone tries to write a __getitem__ / get mypy will produce an error (more like a warning) and if they are sure the usage is safe they can ignore the error in this case, right?

KotlinIsland commented 2 weeks ago

yes, that's correct. although it is most definitely an error (albeit with a lengthy note), we don't emit warnings yet

not entirely correct though, the author is entirely free to write a get that does not use any type parameters and opt for object if they wish. you won't receive an error in this case

LeeeeT commented 2 weeks ago

Thanks for the explanation ❤️