Recognize different setter type for properties.#11643
Closed
esoma wants to merge 7 commits intopython:masterfrom
Closed
Recognize different setter type for properties.#11643esoma wants to merge 7 commits intopython:masterfrom
esoma wants to merge 7 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
I'm not really sure what's going on with the werkzeug |
Collaborator
|
When mypy switched to modular typeshed, Jukka added something that limits the number of errors mypy outputs, so I'm guessing the new "unused type ignore" errors are pushing out pre-existing errors. While presumably nice for users, let me see if there's a way to switch that behaviour off for mypy-primer |
Collaborator
|
Changed in hauntsaninja/mypy_primer@5d50d01 |
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
This doesn't work when the property definition comes from the cache, working on that. Edit: Fixed. |
This comment has been minimized.
This comment has been minimized.
4 tasks
Member
|
Please, take a look at python/typing#985 |
|
Any update on this? |
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: python-htmlgen (https://github.com/srittau/python-htmlgen)
+ test_htmlgen/element.py:218: error: Unused "type: ignore" comment
+ test_htmlgen/element.py:224: error: Unused "type: ignore" comment
+ test_htmlgen/element.py:225: error: Unused "type: ignore" comment
+ test_htmlgen/element.py:254: error: Unused "type: ignore" comment
urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/connection.py:172: error: Unused "type: ignore" comment
werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/exceptions.py:205: error: Unused "type: ignore" comment
+ src/werkzeug/sansio/response.py:139: error: Unused "type: ignore" comment
+ src/werkzeug/sansio/response.py:151: error: Unused "type: ignore" comment
+ src/werkzeug/wrappers/response.py:740: error: Unused "type: ignore" comment
+ src/werkzeug/test.py:389: error: Unused "type: ignore" comment
+ src/werkzeug/debug/__init__.py:284: error: Incompatible types in assignment (expression has type "None", variable has type "str") [assignment]
sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/logging.py:494: error: Unused "type: ignore" comment
zulip (https://github.com/zulip/zulip)
+ zerver/lib/actions.py:4843: error: Unused "type: ignore" comment
porcupine (https://github.com/Akuli/porcupine)
+ porcupine/plugins/pastebin.py:106: error: Unused "type: ignore" comment
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/arrays/period.py:348: error: Signature of "freq" incompatible with supertype "DatetimeLikeArrayMixin" [override]
+ pandas/core/arrays/period.py:348: note: Superclass:
+ pandas/core/arrays/period.py:348: note: @overload
+ pandas/core/arrays/period.py:348: note: def freq(self) -> Any
+ pandas/core/arrays/period.py:348: note: @overload
+ pandas/core/arrays/period.py:348: note: def freq(self, value: Any) -> Any
+ pandas/core/arrays/period.py:348: note: Subclass:
+ pandas/core/arrays/period.py:348: note: def freq(self) -> BaseOffset
aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/web_fileresponse.py:118: error: Unused "type: ignore" comment
+ aiohttp/web_fileresponse.py:119: error: Unused "type: ignore" comment
+ aiohttp/web_fileresponse.py:263: error: Unused "type: ignore" comment
+ aiohttp/web_fileresponse.py:264: error: Unused "type: ignore" comment
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I hit #3004 so I took a stab at it, it's not super elegant, but properties are kind of hacky already 😄. I hope it's not the case that maintainers want to de-hackify properties before tackling this issue, but I can understand if that's the case.
These changes should be backwards compatible in that plugins using the current
is_property/is_settable_propertynode attributes will still function as they do now. That is, the set type of those "properties" will be the same as the get's return type. This only affects "true" decorated properties.This change makes a particular error more verbose:
In master this emits:
But with this PR it emits:
Because the setter is missing. This is consistent with similar errors (if the getter's signature differs the signature error will emit in both branches), so I considered this acceptable.
A note regarding narrowing, since that has some recent discussion in the issue. I'd describe the narrowing behavior as "unchanged" in that mypy will continue to narrow properties. "assymetric" properties (as they have been described where the getter and setter of a property have different types) will narrow if types overlap. This seems (to me) to be the most reasonable way of maintaining the current narrowing behavior. Code is easier than words:
It may be worth mentioning that the issue mentioned here: #3004 (comment) is not addressed by this PR.