Title: Allow mypy to verify if passed object suits to the type hint of an abstract base class · Issue #14537 · python/mypy · GitHub
Open Graph Title: Allow mypy to verify if passed object suits to the type hint of an abstract base class · Issue #14537 · python/mypy
X Title: Allow mypy to verify if passed object suits to the type hint of an abstract base class · Issue #14537 · python/mypy
Description: Feature Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which inherits also passed parameter to that ...
Open Graph Description: Feature Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which...
X Description: Feature Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which...
Opengraph URL: https://github.com/python/mypy/issues/14537
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow mypy to verify if passed object suits to the type hint of an abstract base class","articleBody":"**Feature**\r\n\r\nVerification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which inherits also passed parameter to that function, a proper verification shall be done.\r\n\r\n**Pitch**\r\n\r\nIn case of overloaded functions with type hint e.g. `validate_enum_list(key: str, value: str, allowed_values: Iterable[str])` and `validate_enum_list(key: str, value: Iterable[str], allowed_values: Iterable[Iterable[str]])` it is not possible without additional implementation of own abstract base class `MyIterable` to differentiate `str` from Iterable[str].\r\n\r\nI have seen https://github.com/python/mypy/issues/11001 but it is still open without any clear resolution. It also mentions https://github.com/python/typing/issues/256 and https://github.com/python/mypy/issues/5090\r\n\r\n***Use Cases***\r\n```python3\r\nfrom typing import Any, Iterable, TypeVar, List, Container\r\nfrom multimethod import multimethod\r\nfrom types import GenericAlias\r\n\r\nT = TypeVar(\"T\")\r\nclass MyIterable(Iterable[T]):\r\n @classmethod\r\n def __subclasshook__(cls, subclass: type) -> bool:\r\n return not issubclass(subclass, str)\r\n\r\n def __class_getitem__(cls, key: Any) -> GenericAlias:\r\n return GenericAlias(cls, key)\r\n\r\n@multimethod\r\ndef validate_enum_list(key: str, value: str, allowed_values: MyIterable[str]) -> None:\r\n print(\"key: str, value: str, allowed_values: MyIterable[str]\")\r\n print(type(value))\r\n print(type(allowed_values))\r\n\r\n@validate_enum_list.register\r\ndef _(key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]) -> None:\r\n print(\"key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]\")\r\n print(type(value))\r\n print(type(allowed_values))\r\n\r\n value = sorted(value) # this causes mypy error\r\n allowed_values = sorted([sorted(item) for item in allowed_values]) # this causes mypy error\r\n\r\nvalidate_enum_list(\"test1\", \"test1\", [\"test1\"])\r\nvalidate_enum_list(\"test2\", [\"test2\"], [[\"test1\"]])\r\n#validate_enum_list(\"test1\", \"test1\", [[\"test1\"]]) # uncommenting this will cause runtime exception which is GOOD, but no mypy error which is also WRONG\r\n\r\ndef get_enum_str(key: str, value: str, allowed_values: MyIterable[str]) -> None:\r\n validate_enum_list(key, value, allowed_values)\r\n print(\"get_enum_str\")\r\n\r\ndef get_enum_str_list(key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]) -> None:\r\n validate_enum_list(key, value, allowed_values)\r\n\r\nget_enum_str(\"test1\", \"test1\", [\"test1\"])\r\nget_enum_str_list(\"test2\", [\"test2\"], [[\"test2\"]])\r\nget_enum_str_list(\"test2\", \"test2\", [[\"test2\"]]) # this causes mypy error\r\n```\r\n\r\nAs commented in the code, line `validate_enum_list(\"test1\", \"test1\", [[\"test1\"]])` does not return an error from mypy, but the same issue is with `singledispatch` decorator which I have already found is a bug.\r\n\r\nP.S. I have found some additional informations regardless [__class_getitem__()](https://docs.python.org/3/reference/datamodel.html#object.__class_getitem__) which is supposed to handle type hinting in such cases, but It is also mentioned that:\r\n>Custom implementations of `__class_getitem__()` on classes defined outside of the standard library may not be understood by third-party type-checkers such as mypy\r\n\r\nSo for now I'm confused as to whether it should be a feature request or a bug...","author":{"url":"https://github.com/Dvergatal","@type":"Person","name":"Dvergatal"},"datePublished":"2023-01-27T13:15:06Z","url":"https://github.com/python/mypy/issues/14537"}
| 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:14f42b56-b190-81e4-1ca0-aa5b1f8fa525 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD6E:11BB24:231DAE3:2FB5D25:6B002868 |
| html-safe-nonce | d0bf895bb738c80180567283c5eb11ecfd006755ebc11b2878e45753e07128ec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDZFOjExQkIyNDoyMzFEQUUzOjJGQjVEMjU6NkIwMDI4NjgiLCJ2aXNpdG9yX2lkIjoiNTE3NzA0NTk0NDIxODE2NzQwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7fac4b8d7bd6d07789f96783b133a6c4e7a9b057e95db6401960f26c22858662 |
| hovercard-subject-tag | issue:1559754202 |
| 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/14537/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0ad0d61beed2a7e4e51f377e2e4d6c6c85879c759c8e3408b2e70bd2f188414b/python/mypy/issues/14537 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0ad0d61beed2a7e4e51f377e2e4d6c6c85879c759c8e3408b2e70bd2f188414b/python/mypy/issues/14537 |
| og:image:alt | Feature Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Dvergatal |
| 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