Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covariant Mappings key type? #803

Closed
LeeeeT opened this issue Nov 4, 2024 · 4 comments
Closed

Covariant Mappings key type? #803

LeeeeT opened this issue Nov 4, 2024 · 4 comments

Comments

@LeeeeT
Copy link

LeeeeT commented Nov 4, 2024

Describe the problem

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

Gist to reproduce

No response

Severity

annoying but i can live with it

Your Environment

No response

@LeeeeT LeeeeT added the bug Something isn't working label Nov 4, 2024
@KotlinIsland
Copy link
Owner

KotlinIsland commented Nov 4, 2024

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

@KotlinIsland KotlinIsland added question awaiting response and removed bug Something isn't working labels Nov 5, 2024
@LeeeeT
Copy link
Author

LeeeeT commented Nov 5, 2024

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
Copy link
Owner

KotlinIsland commented Nov 5, 2024

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
Copy link
Author

LeeeeT commented Nov 5, 2024

Thanks for the explanation ❤️

@LeeeeT LeeeeT closed this as completed Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants