Title: Properties, `setter`, and `Incompatible types in assignment` · Issue #4167 · python/mypy · GitHub
Open Graph Title: Properties, `setter`, and `Incompatible types in assignment` · Issue #4167 · python/mypy
X Title: Properties, `setter`, and `Incompatible types in assignment` · Issue #4167 · python/mypy
Description: Expected Behaviour Ability to use property setter with Union[] The intention is to overload the setter Actual Behaviour & Repro Case Python Code from typing import Union class A: def __init__(self) -> None: self._foo = 0 @property def fo...
Open Graph Description: Expected Behaviour Ability to use property setter with Union[] The intention is to overload the setter Actual Behaviour & Repro Case Python Code from typing import Union class A: def __init__(self)...
X Description: Expected Behaviour Ability to use property setter with Union[] The intention is to overload the setter Actual Behaviour & Repro Case Python Code from typing import Union class A: def __init__(s...
Opengraph URL: https://github.com/python/mypy/issues/4167
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Properties, `setter`, and `Incompatible types in assignment`","articleBody":"# Expected Behaviour\r\n- Ability to use property `setter` with `Union[]`\r\n- The intention is to overload the `setter`\r\n\r\n# Actual Behaviour & Repro Case\r\n## Python Code\r\n```python\r\nfrom typing import Union\r\n\r\n\r\nclass A:\r\n def __init__(self) -> None:\r\n self._foo = 0\r\n\r\n @property\r\n def foo(self) -> int:\r\n return self._foo\r\n\r\n @foo.setter\r\n def foo(self, value: Union[int, float]) -> None:\r\n self._foo = int(value)\r\n\r\n\r\nif __name__ == '__main__':\r\n a = A()\r\n print(a.foo)\r\n\r\n a.foo = 123\r\n print(a.foo)\r\n\r\n a.foo = 456.789 # error thrown here\r\n print(a.foo)\r\n```\r\n\r\n## Stdout\r\n```\r\n0\r\n123\r\n456\r\n```\r\n(i.e., valid Python code)\r\n\r\n## Mypy CLI\r\n```\r\nmypy --strict\r\n```\r\n\r\n## Result\r\n```\r\nerror: Incompatible types in assignment (expression has type \"float\", variable has type \"int\")\r\n```\r\n\r\n# Notes\r\n- `Mypy` seems to be type checking the assignment using the type declared in `def foo(self) -> int:` instead of the declared types in the `setter`: ` def foo(self, value: Union[int, float]) -> None:`\r\n\r\n# Current Solution\r\n- Silence the error with `# type: ignore`","author":{"url":"https://github.com/engnadeau","@type":"Person","name":"engnadeau"},"datePublished":"2017-10-26T21:21:52Z","url":"https://github.com/python/mypy/issues/4167"}
| 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:636f909a-c5f5-3158-6216-f26105d45baf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9A6:12476C:138545A:1978025:6B000545 |
| html-safe-nonce | 4f7b723ca613369760e502762187c541cea19f8b9ddddb08b3d26d11b367eeb1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUE2OjEyNDc2QzoxMzg1NDVBOjE5NzgwMjU6NkIwMDA1NDUiLCJ2aXNpdG9yX2lkIjoiMzM5OTY0MDczNDU1MzM0MzMwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7b37f77625115a0723814c948fc9e4bd341e6390c25cd93458de7e721c7b1894 |
| hovercard-subject-tag | issue:268915488 |
| 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/4167/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1508aecfdff9e35cb8a3dfba6cf665da250c92d35a3e76cacf463f92837330ff/python/mypy/issues/4167 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1508aecfdff9e35cb8a3dfba6cf665da250c92d35a3e76cacf463f92837330ff/python/mypy/issues/4167 |
| og:image:alt | Expected Behaviour Ability to use property setter with Union[] The intention is to overload the setter Actual Behaviour & Repro Case Python Code from typing import Union class A: def __init__(self)... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | engnadeau |
| 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