Title: TypeVar in generic union member gets restricted to first constraint · Issue #3644 · python/mypy · GitHub
Open Graph Title: TypeVar in generic union member gets restricted to first constraint · Issue #3644 · python/mypy
X Title: TypeVar in generic union member gets restricted to first constraint · Issue #3644 · python/mypy
Description: In the below: from typing import TypeVar, Generic, Union AnyStr = TypeVar('AnyStr', str, bytes) class PathLike(Generic[AnyStr]): def __fspath__(self) -> AnyStr: ... def readlink(s: Union[AnyStr, PathLike[AnyStr]]) -> AnyStr: ... class BP...
Open Graph Description: In the below: from typing import TypeVar, Generic, Union AnyStr = TypeVar('AnyStr', str, bytes) class PathLike(Generic[AnyStr]): def __fspath__(self) -> AnyStr: ... def readlink(s: Union[AnyStr, Pa...
X Description: In the below: from typing import TypeVar, Generic, Union AnyStr = TypeVar('AnyStr', str, bytes) class PathLike(Generic[AnyStr]): def __fspath__(self) -> AnyStr: ... def readlink(s: Union...
Opengraph URL: https://github.com/python/mypy/issues/3644
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeVar in generic union member gets restricted to first constraint","articleBody":"In the below:\r\n```\r\nfrom typing import TypeVar, Generic, Union\r\n\r\nAnyStr = TypeVar('AnyStr', str, bytes)\r\n\r\nclass PathLike(Generic[AnyStr]):\r\n def __fspath__(self) -> AnyStr: ...\r\n\r\ndef readlink(s: Union[AnyStr, PathLike[AnyStr]]) -> AnyStr:\r\n ...\r\n\r\nclass BP(PathLike[bytes]):\r\n def __fspath__(self) -> bytes:\r\n return b''\r\n\r\nclass SP(PathLike[str]):\r\n def __fspath__(self) -> str:\r\n return ''\r\n\r\nreveal_type(readlink(''))\r\nreveal_type(readlink(b''))\r\nreveal_type(readlink(BP())) # E: Argument 1 to \"readlink\" has incompatible type \"BP\"; expected \"Union[str, PathLike[str]]\"\r\nreveal_type(readlink(SP()))\r\n```\r\nI ran mypy current master with no extra options.\r\n\r\nThe second to last line should type check and the type should be bytes, but instead mypy somehow expects `Union[str, PathLike[str]]`. If I switch around the order of the constraints on the TypeVar, it complains about the last line instead.\r\n\r\nThe following all make the error go away:\r\n- Changing the argument type of readlink to `Union[str, PathLike[AnyStr]]`\r\n- Changing the argument type to `Union[AnyStr, PathLike]`\r\nChanging the order of the Union doesn't make a difference.\r\n\r\nGuido found this bug in a comment on python/typeshed#1441. I couldn't find any other reports in the tracker, but wasn't sure what to search for.","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2017-07-01T05:14:29Z","url":"https://github.com/python/mypy/issues/3644"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:673c00b0-57bf-df86-f985-7f3328a038d7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D878:144E47:30490B2:3FD2AB4:6B002878 |
| html-safe-nonce | 592327d6f9a522265b7a71a30c90454d51cfd7407b026d7e4a1ec152fc075e04 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODc4OjE0NEU0NzozMDQ5MEIyOjNGRDJBQjQ6NkIwMDI4NzgiLCJ2aXNpdG9yX2lkIjoiNjY5Njg5Njc2MTMwNDE5NzI0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5eeaee36328d3cce21438bf74d647a5dd9c5093b3ad39c8a59f76c96c55d4320 |
| hovercard-subject-tag | issue:239927883 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/3644/issue_layout |
| twitter:image | https://opengraph.githubassets.com/275c1af9017ada989533659909eb19aabd09f355a7d9dc6e0f582bce2c5da7c5/python/mypy/issues/3644 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/275c1af9017ada989533659909eb19aabd09f355a7d9dc6e0f582bce2c5da7c5/python/mypy/issues/3644 |
| og:image:alt | In the below: from typing import TypeVar, Generic, Union AnyStr = TypeVar('AnyStr', str, bytes) class PathLike(Generic[AnyStr]): def __fspath__(self) -> AnyStr: ... def readlink(s: Union[AnyStr, Pa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width