Skip to content

Make several fields read-only for type, staticmethod and classmethod #7423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2022

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 2, 2022

All of these are read-only at runtime.

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

pandera (https://github.com/pandera-dev/pandera)
+ pandera/checks.py:74: error: Argument 1 to "ChainMap" has incompatible type "MappingProxyType[str, Any]"; expected "MutableMapping[str, Callable[..., Any]]"  [arg-type]
+ pandera/model.py:362: error: Need type annotation for "attrs" (hint: "attrs: Dict[<type>, <type>] = ...")  [var-annotated]

pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/class_validators.py:332: error: Need type annotation for "all_attributes"  [var-annotated]
+ pydantic/class_validators.py:332: error: Argument 1 to "ChainMap" has incompatible type "*List[MappingProxyType[str, Any]]"; expected "MutableMapping[<nothing>, <nothing>]"  [arg-type]

@AlexWaygood
Copy link
Member Author

Diff from mypy_primer, showing the effect of this PR on open source code:

pandera (https://github.com/pandera-dev/pandera)
+ pandera/checks.py:74: error: Argument 1 to "ChainMap" has incompatible type "MappingProxyType[str, Any]"; expected "MutableMapping[str, Callable[..., Any]]"  [arg-type]
+ pandera/model.py:362: error: Need type annotation for "attrs" (hint: "attrs: Dict[<type>, <type>] = ...")  [var-annotated]

pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/class_validators.py:332: error: Need type annotation for "all_attributes"  [var-annotated]
+ pydantic/class_validators.py:332: error: Argument 1 to "ChainMap" has incompatible type "*List[MappingProxyType[str, Any]]"; expected "MutableMapping[<nothing>, <nothing>]"  [arg-type]

Unfortunate. I think this has more to do with the stub for ChainMap (specifically, it's hard for type checkers to reason about the class given that the maps could be mutable or immutable) than the proposed changes to type.__dict__ in this PR. Nevertheless, maybe it would be better to go back to using

@property
def __dict__(self) -> dict[str, Any]

for type.__dict__.

What do you think, @BvB93?

@BvB93
Copy link
Contributor

BvB93 commented Mar 2, 2022

In my opinion this is more of a problem with ChainMap potentially being too strict (well, this depends a bit on whether or not one is interested in mutating the ChainMap I suppose...), rather than an issue with type.__dict__ itself.

While the discussion on whether ChainMap should or should not accept immutable mappings is a bit more philosophical in nature, setting the return type of type.__dict__ to a dict[str, Any] on the other end is just straight up incorrect based on runtime behavior, especially since types.MappingProxy is neither a sub- nor a supertype of dict.

@AlexWaygood
Copy link
Member Author

I suppose this was discussed in depth in:

and the decision then was that this kind of mypy_primer hit was acceptable.

@JelleZijlstra JelleZijlstra merged commit 1a2f718 into python:master Mar 6, 2022
@AlexWaygood AlexWaygood deleted the patch-3 branch March 6, 2022 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants