Title: mypy false positive with custom property descriptors · Issue #7153 · python/mypy · GitHub
Open Graph Title: mypy false positive with custom property descriptors · Issue #7153 · python/mypy
X Title: mypy false positive with custom property descriptors · Issue #7153 · python/mypy
Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues Please provide more information to help ...
Open Graph Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ...
X Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ...
Opengraph URL: https://github.com/python/mypy/issues/7153
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy false positive with custom property descriptors ","articleBody":"\r\nNote: if you are reporting a wrong signature of a function or a class in\r\nthe standard library, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n\r\nPlease provide more information to help us understand the issue:\r\n\r\n* Are you reporting a bug, or opening a feature request? \r\n\r\nBug.\r\n\r\n* Please insert below the code you are checking with mypy,\r\n or a mock-up repro if the source is private. We would appreciate\r\n if you try to simplify your case to a minimal repro.\r\n\r\nSee below.\r\n\r\n* What is the actual behavior/output?\r\n\r\nSee below.\r\n\r\n* What is the behavior/output you expect?\r\n\r\nError as shown n output should not be reported.\r\n\r\n* What are the versions of mypy and Python you are using?\r\n\r\nmypy 0.711, python 3.7.3\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\n\r\nNone.\r\n\r\n--------------------------------------------------------------------------------------\r\n\r\nIt doesn't seem to be possible to create an explicit descriptor class that behaves\r\nthe same way as @Property does with respect to methods of sub-types declaring more constrained return types.\r\n\r\nIn the example below, we have classes \"Obj\" > \"Foo\", and \"AA\" > \"BB\".\r\nAll methods of Obj return AA. Foo overrides all methods of Obj, but all of those methods\r\nreturn \"BB\". Note that all methods of Foo are still consistent with the definitions in\r\nObj.\r\n\r\nThe method func1 has no decorators and is accepted without complaints, and its types are correctly recognized.\r\n\r\nMethod func3 is decorated with \"@Property\". Here too, mypy handles this case without error.\r\n\r\nMethod func2 is decorated with \"@prop\", which is a custom property descriptor.\r\nThe revealed types for func2 are consistent with func3 -- hence the typing for the \"prop\" class appears to be consistent with what \"@property\" does. However, mypy flags an error that\r\nFoo.func2 is not consistent with the super type definition Obj.func2. (As a related issue, I haven't been able to come up with a better typing scheme for \"prop\" without using \"type:ignore\", which still gives the same type results.)\r\n\r\nIn this example below, I'd expect mypy to report no errors on either func2 or func3, or to report errors for both. As it stands, it appears that it's not possible to create a typing scheme for a descriptor that fully mirrors mypy's special handling for \"@property\".\r\n\r\n```\r\n\r\nfrom typing import Generic, TypeVar,Type, Callable,Any, Union, overload,Optional\r\n\r\nOwnerType = TypeVar(\"OwnerType\", bound=object)\r\nT = TypeVar('T', covariant=True)\r\nCallableType = TypeVar(\"CallableType\", bound=Callable)\r\n\r\n\r\nclass prop(Generic[T]):\r\n def __init__(self, func: Callable[[Any], T]):\r\n self.func = func\r\n\r\n def __get__(self, obj: Any, owner: Type[Any]) -> T:\r\n if obj is None:\r\n return self # type: ignore\r\n return self.func(obj)\r\n\r\nclass AA:\r\n def foo(self) -> int:\r\n return 1\r\n\r\nclass BB(AA):\r\n def bar(self) -> int:\r\n return 1\r\n\r\n \r\nclass Obj:\r\n def func1(self) -> AA:\r\n return AA()\r\n @prop\r\n def func2(self) -> AA:\r\n return AA()\r\n @property\r\n def func3(self) -> AA:\r\n return AA() \r\n\r\nclass Foo(Obj):\r\n def func1(self) -> BB:\r\n return BB()\r\n @prop\r\n def func2(self) -> BB:\r\n return BB()\r\n @property\r\n def func3(self) -> BB:\r\n return BB() \r\n\r\n\r\nx = Obj()\r\ny = Foo()\r\nreveal_type(Obj.func1)\r\nreveal_type(Obj.func2)\r\nreveal_type(Obj.func3)\r\nreveal_type(Foo.func1)\r\nreveal_type(Foo.func2)\r\nreveal_type(Foo.func3)\r\nreveal_type(x.func1)\r\nreveal_type(x.func2)\r\nreveal_type(x.func3)\r\nreveal_type(y.func1)\r\nreveal_type(y.func2)\r\nreveal_type(y.func3)\r\n```\r\nMypy output:\r\n\r\n/tmp/xx.py:40: error: Signature of \"func2\" incompatible with supertype \"Obj\"\r\n/tmp/xx.py:49: note: Revealed type is 'def (self: xx.Obj) -> xx.AA'\r\n/tmp/xx.py:50: note: Revealed type is 'xx.AA*'\r\n/tmp/xx.py:51: note: Revealed type is 'def (self: xx.Obj) -> xx.AA'\r\n/tmp/xx.py:52: note: Revealed type is 'def (self: xx.Foo) -> xx.BB'\r\n/tmp/xx.py:53: note: Revealed type is 'xx.BB*'\r\n/tmp/xx.py:54: note: Revealed type is 'def (self: xx.Foo) -> xx.BB'\r\n/tmp/xx.py:55: note: Revealed type is 'def () -> xx.AA'\r\n/tmp/xx.py:56: note: Revealed type is 'xx.AA*'\r\n/tmp/xx.py:57: note: Revealed type is 'xx.AA'\r\n/tmp/xx.py:58: note: Revealed type is 'def () -> xx.BB'\r\n/tmp/xx.py:59: note: Revealed type is 'xx.BB*'\r\n/tmp/xx.py:60: note: Revealed type is 'xx.BB'","author":{"url":"https://github.com/mostrows2","@type":"Person","name":"mostrows2"},"datePublished":"2019-07-04T17:40:13Z","url":"https://github.com/python/mypy/issues/7153"}
| 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:2d813246-2ec0-394f-46da-2bc910c6185f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D022:2E6021:240FE47:2F03016:6AFFF26B |
| html-safe-nonce | dd44d8a1e3147526354ebcf350041dc84af540a289d79e40fce456eeb9abb2e4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDIyOjJFNjAyMToyNDBGRTQ3OjJGMDMwMTY6NkFGRkYyNkIiLCJ2aXNpdG9yX2lkIjoiNDQzNzMwOTM4NTkzMjI3MjIzNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2bef18b9b7e0334d222e981252d78d8b2232d3cb87cd6b5134d7c4416077ad1d |
| hovercard-subject-tag | issue:464356820 |
| 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/7153/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90c8e38b5532ea5bd6a5fcf4e162feec47787878560781cc3759e4bd2e4ce0b7/python/mypy/issues/7153 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90c8e38b5532ea5bd6a5fcf4e162feec47787878560781cc3759e4bd2e4ce0b7/python/mypy/issues/7153 |
| og:image:alt | Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mostrows2 |
| 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