Title: Unconstrained type variable combined with isinstance() doesn't seem to work · Issue #1539 · python/mypy · GitHub
Open Graph Title: Unconstrained type variable combined with isinstance() doesn't seem to work · Issue #1539 · python/mypy
X Title: Unconstrained type variable combined with isinstance() doesn't seem to work · Issue #1539 · python/mypy
Description: Not sure this is a bug or not... from typing import TypeVar S = TypeVar('S') def parenthesize(s: S) -> S: if isinstance(s, str): return '(' + s + ')' elif isinstance(s, bytes): return b'(' + s + b')' else: raise TypeError("That's not a s...
Open Graph Description: Not sure this is a bug or not... from typing import TypeVar S = TypeVar('S') def parenthesize(s: S) -> S: if isinstance(s, str): return '(' + s + ')' elif isinstance(s, bytes): return b'(' + s + b'...
X Description: Not sure this is a bug or not... from typing import TypeVar S = TypeVar('S') def parenthesize(s: S) -> S: if isinstance(s, str): return '(' + s + ')' elif isinstance(s, b...
Opengraph URL: https://github.com/python/mypy/issues/1539
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unconstrained type variable combined with isinstance() doesn't seem to work","articleBody":"Not sure this is a bug or not...\n\n```\nfrom typing import TypeVar\n\nS = TypeVar('S')\n\ndef parenthesize(s: S) -> S:\n if isinstance(s, str):\n return '(' + s + ')'\n elif isinstance(s, bytes):\n return b'(' + s + b')'\n else:\n raise TypeError(\"That's not a string, dude! It's a %s\" % type(s))\n```\n\nThis gets errors on both `return` lines:\n\n```\ndemo.py: note: In function \"parenthesize\":\ndemo.py:7: error: Incompatible return value type: expected S`-1, got builtins.str\ndemo.py:9: error: Incompatible return value type: expected S`-1, got builtins.bytes\n```\n\nWhy? The isinstance() checks look like they should provide sufficient guards.\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2016-05-17T03:34:35Z","url":"https://github.com/python/mypy/issues/1539"}
| 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:22111895-ce7b-3616-2ff7-6a538fccd14e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFF8:1DD784:4CDD64:67106C:6B002ECF |
| html-safe-nonce | a4b30c2e03728ac524566690f60fa130c81b7464b31f191772f4bd4a9139c236 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkY4OjFERDc4NDo0Q0RENjQ6NjcxMDZDOjZCMDAyRUNGIiwidmlzaXRvcl9pZCI6IjM1NjkyODY5MDA0NTM3NDg0MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e1916554ebc9b0bb5e8dd891d27a930193abe8e964eff209ad1042d28961b9e5 |
| hovercard-subject-tag | issue:155169952 |
| 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/1539/issue_layout |
| twitter:image | https://opengraph.githubassets.com/13f5ea6cb445e89dd65882d0977730144055bb3f15309387592f541c69bcab6a/python/mypy/issues/1539 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/13f5ea6cb445e89dd65882d0977730144055bb3f15309387592f541c69bcab6a/python/mypy/issues/1539 |
| og:image:alt | Not sure this is a bug or not... from typing import TypeVar S = TypeVar('S') def parenthesize(s: S) -> S: if isinstance(s, str): return '(' + s + ')' elif isinstance(s, bytes): return b'(' + s + b'... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width