Title: Type parameter scoping seems to disagree with PEP 484 · Issue #11993 · python/mypy · GitHub
Open Graph Title: Type parameter scoping seems to disagree with PEP 484 · Issue #11993 · python/mypy
X Title: Type parameter scoping seems to disagree with PEP 484 · Issue #11993 · python/mypy
Description: PEP 484 has: "A type variable used in a method of a generic class that coincides with one of the variables that parameterize this class is always bound to that variable." The example there has: T = TypeVar('T') class MyClass(Generic[T]):...
Open Graph Description: PEP 484 has: "A type variable used in a method of a generic class that coincides with one of the variables that parameterize this class is always bound to that variable." The example there has: T =...
X Description: PEP 484 has: "A type variable used in a method of a generic class that coincides with one of the variables that parameterize this class is always bound to that variable." The example ther...
Opengraph URL: https://github.com/python/mypy/issues/11993
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type parameter scoping seems to disagree with PEP 484","articleBody":"PEP 484 has: \"A type variable used in a method of a generic class that coincides with one of the variables that parameterize this class is always bound to that variable.\" The example there has:\r\n\r\n```python\r\nT = TypeVar('T')\r\n\r\nclass MyClass(Generic[T]):\r\n def meth_1(self, x: T) -> T: ...\r\n```\r\n\r\nThis should mean that T occurs bound in the signature of meth_1 (it is bound to the type parameter of the generic class MyClass).\r\nThe signature of MyClass[int].meth_1 should be `(self:MyClass[int],x:int)->int`. This is gotten from the signature of the function in the generic class with the type argument int substituted for occurrences of the type parameter T.\r\n\r\nThe signature of MyClass.meth_1 should be `(self:MyClass[Any],x:Any)->Any`. The generic class is implicitly instantiated to Any. Further, we must substitute something for the occurrences of the type parameter in the signature (the type parameter can't become a free occurrence just by looking at it from outside the class).\r\n\r\nMypy seems to do something else. The type of MyClass[int].meth_1 is revealed as:\r\n\r\n```python\r\ndef (self: test.MyClass[T`1], x: T`1) -> T`1\r\n```\r\n\r\nInstead of substituting int for T it has used some other type (maybe a free type variable?). If we try to apply it to an instance of MyClass[int] and an int, we get that the type of the application is T\\`1 whatever that is, and we get errors:\r\n\r\n```\r\nerror: Argument 1 has incompatible type \"MyClass[int]\"; expected \"MyClass[T]\"\r\nerror: Argument 2 has incompatible type \"int\"; expected \"T\"\r\n```\r\n\r\nThe type of MyClass.meth_1 is revealed as:\r\n\r\n```python\r\ndef [T] (self: test.MyClass[T`1], x: T`1) -> T`1\r\n```\r\n\r\nThis looks like it might be a generic function, but MyClass.meth_1 should not be generic (it has no free type variables). There is actually no occurrence of the type parameter T in the signature, it seems to have been instantiated to T\\`1 whatever that is (unless T\\`1 is another spelling of T).\r\n\r\nThis function does behave as if it were generic, actually. We can apply it to an instance of MyClass[int] and an int and the type of the application is int. And it's apparently not an instantiation to Any because if we try to apply it to an instance of MyClass[int] and a str it is a type error \"Cannot infer function argument\".\r\n\r\nWhat I think is happening is that mypy is treating functions in generic classes as *themselves* generic functions parameterized over the class's type parameters. This seems wrong or else I've completely misunderstood PEP 484.\r\n","author":{"url":"https://github.com/kmillikin","@type":"Person","name":"kmillikin"},"datePublished":"2022-01-14T12:28:19Z","url":"https://github.com/python/mypy/issues/11993"}
| 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:6c5d10f6-b2ac-9169-6cac-5c11bfc112e3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9E4:1833B7:19DEE10:22C1510:6AFFF951 |
| html-safe-nonce | 4443f5d55fd3c93aad39f035328e024c99171ff07f4c311e21511cfad4a994f5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUU0OjE4MzNCNzoxOURFRTEwOjIyQzE1MTA6NkFGRkY5NTEiLCJ2aXNpdG9yX2lkIjoiNjg0OTAwMDM5MDk4MjEwNTQyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1d1be71a325fffd7b26db6cd91a039ded743b98fae13c9c6c5e34af57d8545fb |
| hovercard-subject-tag | issue:1103569070 |
| 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/11993/issue_layout |
| twitter:image | https://opengraph.githubassets.com/044d90fcc3cc820127e99bdc03b30b2c300d8446aed3fcc931e2adc7cc77ec12/python/mypy/issues/11993 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/044d90fcc3cc820127e99bdc03b30b2c300d8446aed3fcc931e2adc7cc77ec12/python/mypy/issues/11993 |
| og:image:alt | PEP 484 has: "A type variable used in a method of a generic class that coincides with one of the variables that parameterize this class is always bound to that variable." The example there has: T =... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kmillikin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width