Title: `type` builtin function should return an instance of `typing.Type`? · Issue #1758 · python/mypy · GitHub
Open Graph Title: `type` builtin function should return an instance of `typing.Type`? · Issue #1758 · python/mypy
X Title: `type` builtin function should return an instance of `typing.Type`? · Issue #1758 · python/mypy
Description: I've run into a snippit of code which essentially boils down into this: class SomeObject(object): @classmethod def test_class_method(cls) -> None: print('Test class method') @staticmethod def test_static_method() -> None: print('Test sta...
Open Graph Description: I've run into a snippit of code which essentially boils down into this: class SomeObject(object): @classmethod def test_class_method(cls) -> None: print('Test class method') @staticmethod def test_...
X Description: I've run into a snippit of code which essentially boils down into this: class SomeObject(object): @classmethod def test_class_method(cls) -> None: print('Test class method') @staticm...
Mail addresses
notifications@github.com
Opengraph URL: https://github.com/python/mypy/issues/1758
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`type` builtin function should return an instance of `typing.Type`?","articleBody":"I've run into a snippit of code which essentially boils down into this:\n\n``` python\nclass SomeObject(object):\n @classmethod\n def test_class_method(cls) -> None:\n print('Test class method')\n\n @staticmethod\n def test_static_method() -> None:\n print('Test static method')\n\ns = SomeObject()\nt = type(s)\n\n# Doesn't typecheck\nt.test_static_method()\nt.test_class_method()\n```\n\nThe behavior I was expecting was for `t` to be of type `Type[SomeObject]` -- instead, it's of type `type`, due to how `type` is annotated in Typeshed:\n\n``` python\nclass type:\n __bases__ = ... # type: Tuple[type, ...]\n __name__ = ... # type: str\n __qualname__ = ... # type: str\n __module__ = ... # type: str\n __dict__ = ... # type: Dict[str, Any]\n __mro__ = ... # type: Tuple[type, ...]\n\n @overload\n def __init__(self, o: object) -> None: ...\n @overload\n def __init__(self, name: str, bases: Tuple[type, ...], dict: Dict[str, Any]) -> None: ...\n @overload\n def __new__(cls, o: object) -> type: ...\n @overload\n def __new__(cls, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> type: ...\n def __call__(self, *args: Any, **kwds: Any) -> Any: ...\n def __subclasses__(self) -> List[type]: ...\n # Note: the documentation doesn't specify what the return type is, the standard\n # implementation seems to be returning a list.\n def mro(self) -> List[type]: ...\n```\n\nI attempted modifying the definition in Typeshed by first changing `__new__` to have the signature `def __new__(cls, o: _T) -> Type[_T]`, but that had no effect. I then tried commenting out both `__init__`s entirely in case mypy was defaulting to looking at those, but that also did nothing. I'm not entirely sure why the change to `__new__` isn't working, though it may be related to [issue 1020](https://github.com/python/mypy/issues/1020)?\n\nI did find a workaround for this particular case by doing:\n\n``` python\nfrom typing import cast, Type\n\n# ...snip...\n\ns = SomeObject()\nt2 = cast(Type[SomeObject], type(s))\n\n# typechecks\nt2.test_static_method()\nt2.test_class_method()\n```\n\n...but it would be nice if `type` could be changed so this casting is unnecessary, though I'm not entirely sure how this would be done.\n","author":{"url":"https://github.com/Michael0x2a","@type":"Person","name":"Michael0x2a"},"datePublished":"2016-06-28T04:08:30Z","url":"https://github.com/python/mypy/issues/1758"}
| 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:ad6795e6-e445-af01-95cf-2934b8e87f3a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B4F6:07D0:1191A81:1709222:6B000CB1 |
| html-safe-nonce | c3966f1edb4e80fc0294398f6e8726e263e6985fad197d3f419874321e44b7b2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNEY2OjA3RDA6MTE5MUE4MToxNzA5MjIyOjZCMDAwQ0IxIiwidmlzaXRvcl9pZCI6IjU1NjQzMzA2NDAwMTY0MTE4MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 19e7b86e04c0c89a630759876de03639813c577ab7e3146259f3987eee8277bf |
| hovercard-subject-tag | issue:162598382 |
| 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/1758/issue_layout |
| twitter:image | https://opengraph.githubassets.com/688e39ee43a8b962fe7dde10956e9c32ac458b2d2306cb6324f24f5da90c534d/python/mypy/issues/1758 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/688e39ee43a8b962fe7dde10956e9c32ac458b2d2306cb6324f24f5da90c534d/python/mypy/issues/1758 |
| og:image:alt | I've run into a snippit of code which essentially boils down into this: class SomeObject(object): @classmethod def test_class_method(cls) -> None: print('Test class method') @staticmethod def test_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Michael0x2a |
| hostname | github.com |
| expected-hostname | github.com |
| None | cf7ee0062053ad9eb05b6e5de57bd96c2055708bcbada4144946b5ec129cf1d7 |
| 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 | 9cab7de176a56362d2aabcbb4d1e07e846eba8a0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width