Title: Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]` · Issue #16174 · python/mypy · GitHub
Open Graph Title: Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]` · Issue #16174 · python/mypy
X Title: Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]` · Issue #16174 · python/mypy
Description: Bug Report Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as a regression, bisected to 391ed853f (PR ...
Open Graph Description: Bug Report Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as ...
X Description: Bug Report Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as ...
Opengraph URL: https://github.com/python/mypy/issues/16174
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]`","articleBody":"\u003c!--\r\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\r\nPlease consider:\r\n\r\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\r\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\r\n- asking on gitter chat: https://gitter.im/python/typing\r\n-->\r\n\r\n**Bug Report**\r\n\r\n\u003c!--\r\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\r\n\r\nIf the project you encountered the issue in is open source, please provide a link to the project.\r\n-->\r\nDefault (`default=`) value for `attrs.field()` type is revealed as `Union[builtins.int, None]` where the Union with None is unexpected. While this used to be revealed as `Any`, this shows up as a regression, bisected to https://github.com/python/mypy/commit/391ed853f (PR https://github.com/python/mypy/pull/15021, mypy master, unreleased).\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nimport attrs\r\n\r\n@attrs.define()\r\nclass MyDataClass:\r\n some_int_field: int = attrs.field(default=123)\r\n\r\n# Somewhere else in the project I would like to reference that default:\r\nreveal = attrs.fields(MyDataClass).some_int_field.default\r\nreveal_type(reveal)\r\n\r\nassignment_to_int_fails: int = attrs.fields(MyDataClass).some_int_field.default\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n\u003c!--\r\nHow did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.\r\nWrite down what you thought would happen. If you expected no errors, delete this section.\r\n-->\r\n\r\n```\r\n$ mypy --strict myfile.py\r\nmyfile.py:9:13: note: Revealed type is \"builtins.int\"\r\nSuccess: no issues found in 1 source file\r\n```\r\n\r\n**Actual Behavior**\r\n\r\non master (tested 9edda9a79790d8f7263234eca9509657ea0c37f0):\r\n```\r\n$ mypy --strict --show-error-context --show-column-numbers myfile.py\r\nmyfile.py:9:13: note: Revealed type is \"Union[builtins.int, None]\"\r\nmyfile.py:11:32: error: Incompatible types in assignment (expression has type \"int | None\", variable has type \"int\") [assignment]\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n(my project fails, suspected mypy bug / imperfect type inference)\r\n\r\nOn 1.5.1:\r\n```\r\n$ mypy --strict myfile.py\r\nmyfile.py:9:13: note: Revealed type is \"Any\"\r\nSuccess: no issues found in 1 source file\r\n```\r\n\r\n(my project passes, albeit without a proper type check)\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: master (1.5.1 = OK, introduced with https://github.com/python/mypy/commit/391ed853f)\r\n- Mypy command-line flags: `--strict --show-error-context --show-column-numbers`\r\n- Mypy configuration options from `mypy.ini` (and other config files): n/a\r\n- Python version used: 3.11.5\r\n\r\nProject to test on if you want (100% mypy-strict compatible on 1.5.1 and master otherwise): https://github.com/gertvdijk/PyKMP\r\n\r\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\r\n","author":{"url":"https://github.com/gertvdijk","@type":"Person","name":"gertvdijk"},"datePublished":"2023-09-24T22:37:16Z","url":"https://github.com/python/mypy/issues/16174"}
| 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:39a74afd-3a8c-a4e3-39fd-d066938dd844 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 83DC:1BAA33:22822EE:2DE1ADF:6B004E7B |
| html-safe-nonce | d6d6ae9c80f5eb230bdf8a41b26c5496e3b40da01bba649d83e97c799723d92e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4M0RDOjFCQUEzMzoyMjgyMkVFOjJERTFBREY6NkIwMDRFN0IiLCJ2aXNpdG9yX2lkIjoiNjUyMTMyMzgzNjg4ODA3NTg5OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 79abb62f3486b99b0dde32b128134a8e95b3e0b393f2f32f5023aaa25a2eb81f |
| hovercard-subject-tag | issue:1910404478 |
| 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/16174/issue_layout |
| twitter:image | https://opengraph.githubassets.com/94e8d5e716a8bee8b43cf38801805d940f093ba54009faeae02d5b5ffb2f51d1/python/mypy/issues/16174 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/94e8d5e716a8bee8b43cf38801805d940f093ba54009faeae02d5b5ffb2f51d1/python/mypy/issues/16174 |
| og:image:alt | Bug Report Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gertvdijk |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width