pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

PEP 604 `isinstance` support (unions should be treated as runtime types)

Open neutrinoceros opened this issue 6 months ago • 2 comments

Describe the Bug

within an isinstance call, the following is valid at runtime (in Python 3.10+)

isinstance(x, str | os.PathLike)

while the following isn't isinstance(x, str | os.PathLike[str])

Under preset = "strict", pyrefly 1.0.0 flags the former and incorrectly suggests the latter. Meanwhile, it (correctly) doesn't flag isinstance(x, os.PathLike), suggesting that it cannot tell that the unions in the first two calls need to be treated as runtime types, which I guess amounts to pyrefly not understanding PEP 604

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABLnADroceYxhOoBjATGIMA%2BnAZ0xxVAwAWYgIwAKYohZxCABTnyAMhADWMANqS6AXQCUiDkwdMIfCHAjpJGYWoA0mnXqGJrZMAMQsRvaO0cR07gwqbCCyClDGMEnWUQ4wUHAwdujR0XSorjBMACqkxDAAonR09FycWLz8QiLiFjJ6YgBMahqsAQpBZhY2hdHOTm4eDF4wvkwWTAA%2B-rpj6eZSU2FMYKh5FegwAOZyEABuGUXFDrHxicl6aSaZ2Uy5%2BdOPTFK5SqNXqjWarW47UEwlEEikvQUYgAzEMtoFdpNbN9Zq53J50N5iH41psRtsDOkQuFjqcmLQ3AxbvcAfS4ugEkkUvIPhkQFkHjlTv9HkD8iDag0mnQQD5kqQ6LwoKRCAxcFQoBRwtoFUrSKsMDgCEwBLh0JALgBXUpMs2EDjhADKMAq8gYDGIcEQAHpvWRFWBlYR6BdvTB0N7MLgBHBvabzRArTaIGbvUd6PwbmUoKhsLATWaLdbrmaWKIUx57egyAozQBaO50NylgC8TCSyMISn6SQ4sXgMGYbaSFggAgYvfQphg4Kblg4lvQ1FojBgmDrmAgivHzKYbYA5In0PQYPuOMeGHXFQBHS1btd1kykOsw%2BBwPdMfcAd1QdHQZ-QEAAF85UEJk7gAMWgGAKDQLA8CIMhgKAA

(Only applicable for extension issues) IDE Information

No response

neutrinoceros avatar May 15 '26 12:05 neutrinoceros

Thanks for the report, @neutrinoceros!

Confirmed this is a bug. Under strict mode, isinstance(x, str | os.PathLike) incorrectly fires implicit-any-type-argument because pyrefly processes the | operator before it knows the expression is inside isinstance. The | handler tries to fill in os.PathLike's missing type parameter with implicit Any, which strict mode treats as an error.

Notably, isinstance(x, os.PathLike) alone works fine, the dedicated isinstance handler knows bare generics are valid there. The issue is only triggered when | is involved.

Additionally, pyrefly's suggestion to use os.PathLike[str] would cause a TypeError at runtime because Python's isinstance() does not accept parameterized generics as classinfo.

We're tracking this for a fix. Tagging @stroxler.

NathanTempest avatar May 15 '26 16:05 NathanTempest

This issue has someone assigned, but has not had recent activity for more than 2 weeks.

If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.

Thank you for your contributions!

github-actions[bot] avatar Jun 20 '26 01:06 github-actions[bot]