Title: mypy should understand that setters can accept wider types than their getter returns · Issue #19057 · python/mypy · GitHub
Open Graph Title: mypy should understand that setters can accept wider types than their getter returns · Issue #19057 · python/mypy
X Title: mypy should understand that setters can accept wider types than their getter returns · Issue #19057 · python/mypy
Description: Bug Report Apologies in advance if this has already been reported. I tried to search the tracker but my search-fu must not be good enough. mypy should allow property setters to accept compatible but wider types than their property getter...
Open Graph Description: Bug Report Apologies in advance if this has already been reported. I tried to search the tracker but my search-fu must not be good enough. mypy should allow property setters to accept compatible bu...
X Description: Bug Report Apologies in advance if this has already been reported. I tried to search the tracker but my search-fu must not be good enough. mypy should allow property setters to accept compatible bu...
Opengraph URL: https://github.com/python/mypy/issues/19057
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy should understand that setters can accept wider types than their getter returns","articleBody":"**Bug Report**\n\nApologies in advance if this has already been reported. I tried to search the tracker but my search-fu must not be good enough.\n\nmypy should allow property setters to accept compatible but wider types than their property getter returns. mypy 1.50.0 returns this error:\n\n```\nlock.py:11: error: Incompatible types in assignment (expression has type \"timedelta | int\", variable has type \"timedelta\") [assignment]\nFound 1 error in 1 file (checked 1 source file)\n```\n\nbut it should be allowed, since `self._lifetime` is always guaranteed to be a `timedelta` and therefore the getter's return type should always be correct.\n\n**To Reproduce**\n\n```python\nfrom datetime import timedelta\n\ntype Interval = timedelta | int\n\nDEFAULT_LIFETIME = timedelta(seconds=3)\n\n\nclass Lock:\n def __init__(self, lifetime: Interval | None = None):\n self._lifetime: timedelta\n self.lifetime = DEFAULT_LIFETIME if lifetime is None else lifetime\n\n @property\n def lifetime(self) -> timedelta:\n return self._lifetime\n\n @lifetime.setter\n def lifetime(self, lifetime: Interval) -> None:\n if isinstance(lifetime, timedelta):\n self._lifetime = lifetime\n else:\n self._lifetime = timedelta(seconds=lifetime)\n\n```\n\n**Expected Behavior**\n\nI wouldn't expect mypy to complain about line 11.\n\n**Actual Behavior**\n\nSee above for the error. It's easy enough to add a `typing: ignore[assignment]` to line 11, but that doesn't seem like it should be necessary.\n\n**Your Environment**\n\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\n\n- Mypy version used: 1.15.0 (compiled: yes)\n- Mypy command-line flags: `mypy lock.py`\n- Mypy configuration options from `mypy.ini` (and other config files): None\n- Python version used: Python 3.13.3\n\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/warsaw","@type":"Person","name":"warsaw"},"datePublished":"2025-05-08T20:46:58Z","url":"https://github.com/python/mypy/issues/19057"}
| 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:a31bcafd-05ab-79d4-bfc7-7656acca345f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5AC:30263F:2201E02:2E4626D:6B002815 |
| html-safe-nonce | 3eea5b58530c5dd887af7193261ab4aa7f7484f1ff4d12c48704e62ba56b90d7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUFDOjMwMjYzRjoyMjAxRTAyOjJFNDYyNkQ6NkIwMDI4MTUiLCJ2aXNpdG9yX2lkIjoiNDk4NTY4NTM2NDI2NzAzNDY0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 28f6ea0dddca2c2c4227f6fed47e64f69e7fe87a9f4801c2f918e9eb0e23b527 |
| hovercard-subject-tag | issue:3050058644 |
| 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/19057/issue_layout |
| twitter:image | https://opengraph.githubassets.com/02dc8e2b3b95e30bb1e17e5ef2e5966b3237d521a70d20b6159f9a37f0ec1913/python/mypy/issues/19057 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/02dc8e2b3b95e30bb1e17e5ef2e5966b3237d521a70d20b6159f9a37f0ec1913/python/mypy/issues/19057 |
| og:image:alt | Bug Report Apologies in advance if this has already been reported. I tried to search the tracker but my search-fu must not be good enough. mypy should allow property setters to accept compatible bu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | warsaw |
| 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