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

reportInvalidCast for cast(None, object()): "convert the expression to object first" #568

Closed
jorenham opened this issue Aug 13, 2024 · 3 comments · Fixed by #579
Closed
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@jorenham
Copy link
Contributor

jorenham commented Aug 13, 2024

https://basedpyright.com/?code=JYWwDg9gTgLgBDAnmYA7A5nAhgZwQKBgDoBjXGACgDkJUBTAGjggCMArOkygSm6A

So a couple of months ago I tried to do

import typing as t
t.cast(None, object())

which gave

Conversion of type `object` to type `None` may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to `object` first.  (reportInvalidCast)

So I decided to follow the advice, i.e.:

convert the expression to object first.

... but after I did that, it said it again. So it was obvious to me that I should keep at it for a while.

Then after I had cast object to object for the 3000th time, my brain raised a RecursionError!
So now I'm wondering if maybe there's a chance that there's a bug involved here, or if I should brain.setrecursionlimit(1 << 20) and give it another shot 🤔.
Any thoughts?

@DetachHead DetachHead added the type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules label Aug 13, 2024
@DetachHead
Copy link
Owner

must be some annoying special casing with None causing this. thanks for reporting

@jorenham
Copy link
Contributor Author

It's not a problem to e.g. cast(Never, object()) or cast(Any, object()).

But this issue also appears to be the case for cast(type[T], object()) with T e.g. Any, object, or int. The only except I managed to find was cast(type[Never], object()), which is allowed.

@jorenham
Copy link
Contributor Author

I also found a workaround:

cast(None, cast(Never, object()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants