Skip to content
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

avoid using return t.cast which can prevent attribute access during process teardown #913

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

minrk
Copy link
Member

@minrk minrk commented Nov 28, 2024

return t.cast(..., X) is equivalent to return X # type:ignore on the result of the expression, but (negligibly) more expensive and can cause errors e.g. preventing access to attributes during __del__ in process teardown, as seen in jupyterhub/repo2docker#1379. It's a good idea to try to avoid anything that may prevent access to attribtues. This means trying our best to keep any module.method calls out of the __get__ path (note: importing from typing import cast is another way to avoid this specific issue and works for methods we really do want to use in this code, but the ignores just seem better than the casts to me, anyway).

Note this is only applied to return statements, since the type narrowing of t.cast necessarily has no effect there other than explicitly ignoring a single type check. Other uses of t.cast in the middle of some code for type narrowing are still totally sensible.

@minrk minrk added the bug label Nov 28, 2024
@minrk minrk force-pushed the no-return-cast branch 2 times, most recently from c437575 to c080d5b Compare November 28, 2024 10:52
return cast is equivalent to `type: ignore` on the result of the expression,
but more expensive and can cause errors e.g. preventing access to traits during __del__ in process teardown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant