Skip to content

stdlib: Add several missing @abstractmethod decorators #7443

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 17 commits into from
Mar 7, 2022

Conversation

AlexWaygood
Copy link
Member

The following methods are all abstract at runtime, but not in the stub:

  • typing.ContextManager.__exit__ (and therefore also contextlib.AbstractContextManager.__exit__)
  • typing.AsyncContextManager.__aexit__ (and therefore also contextlib.AbstractAsyncContextManager.__aexit__)
  • numbers.Complex.__eq__
  • numbers.Complex.__abs__
  • numbers.Complex.__conjugate__
  • os.PathLike.__fspath__

This means that inheriting from any of these classes at runtime without overriding these methods would cause an exception to be raised. Type checkers, however, would not have flagged the possibility of an exception being raised, believing these methods to be concrete.

@AlexWaygood AlexWaygood marked this pull request as draft March 5, 2022 14:20
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as ready for review March 5, 2022 14:36
@github-actions

This comment has been minimized.


class Event(AbstractContextManager[bool]):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class doesn't seem to be a context manager at runtime, and doesn't seem to have ever been a context manager at runtime.

AlexWaygood and others added 11 commits March 6, 2022 23:36
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@github-actions

This comment has been minimized.

5 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Mar 7, 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]

These are weird -- looks like this crossed the streams with #7423 being merged, maybe?

@JelleZijlstra
Copy link
Member

The boostedblob errors are unfortunate. They're in places like https://github.com/hauntsaninja/boostedblob/blob/eb6e95b41ba55c4c595cbe754d05a48f069ce42e/boostedblob/path.py#L328, where request.execute is an async CM. The new annotations make mypy catch on to the fact that async CMs can eat exceptions.

And yeah, not sure what caused the ChainMap changes.

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Mar 7, 2022

Oh wait, I bet mypy special cases -> bool as "may swallow" and assumes that "bool | None" means "won't swallow". So we should probably just change _AsyncGeneratorContextManager.__aexit__ back; sorry for the thrash.

@JelleZijlstra
Copy link
Member

And now pyright in CI is complaining about shutil, which you didn't change.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Mar 7, 2022

And now pyright in CI is complaining about shutil, which you didn't change.

Yeah, it's because #7384 made chown unavailable on Windows, but it's still declared in __all__ on Windows. It wasn't picked up by CI running on that PR, because I increased pyright strictness for checking __all__ after that PR was filed.

@github-actions

This comment has been minimized.

JelleZijlstra added a commit that referenced this pull request Mar 7, 2022
JelleZijlstra added a commit that referenced this pull request Mar 7, 2022
@JelleZijlstra
Copy link
Member

Ah good catch

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

2 participants