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

fix: address __slots__ and __all__ misconfigurations #3273

Closed

Conversation

ariebovenberg
Copy link
Contributor

Fixes #3251

Remarks:

  • For the 'ensure dunder all' check, I was unsure what the exact intention is. Running it resulted in many __all__ definitions, which I left unchanged.
  • To properly run slotscheck it needs to import the code, which requires all dependencies. Keeping them up-to-date will probably be a chore. The alternative is to use language: system, but this negates the benefit of pre-commit environment isolation.
  • I wasn't able to get __slots__ working on AbstractMiddleware without making breaking changes. This is due to the fact that the slot names are used both as instance and class variables. To prevent confusion, I've removed the (ineffective) slots from its subclasses
  • A python bug (github.com/gh-105726: Add __slots__ to AbstractContextManager and AbstractAsyncContextManager python/cpython#106771) renders slots on event emitters ineffective. Since this will roll out in Python 3.13, I've kept the slots

@ariebovenberg ariebovenberg requested review from a team as code owners March 27, 2024 21:13
Copy link
Contributor

@peterschutt peterschutt left a comment

Choose a reason for hiding this comment

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

Thanks very much for identifying this issue @ariebovenberg, and for submitting the patch!

I think slotscheck is an important tool for us, and by the looks of this we had a couple of configuration issues (i.e., the exclude regex and improper environment setup). Is it possible for the tool to be run outside of pre-commit? We recently removed mypy and pyright from pre-commit due to having to maintain multiple lists of dependencies which would frequently be forgotten about as the library dependencies changed.

I have particular concern with flake8-dunder-all, firstly the way it injects the __all__ in between the runtime and typing imports, and secondly I feel that the names added to __all__ needs a bit more consideration than writing any non-underscore name in there, especially in our public namespaces.

@litestar-org/maintainers any thoughts on flake8-dunder-all?

.pre-commit-config.yaml Show resolved Hide resolved
Comment on lines +9 to +14
__all__ = (
"AsyncDeque",
"Subscriber",
)


Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, not a fan that flake8-dunder-all injects this in between runtime imports and type-checking imports.

I'm also not sure whether blindly adding any non-underscore names into an __all__ is desirable. For instance in this case, I don't believe AsyncDeque is intended to be part of the modules public interface.

Copy link
Member

Choose a reason for hiding this comment

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

i can think of a few instances we havent privatized a thing and want it undocumented, but im more tilted about how it doesnt place them under TYPE_CHECKING imports as well :D

@@ -21,7 +21,7 @@
class RedisStore(NamespacedStore):
"""Redis based, thread and process safe asynchronous key/value store."""

__slots__ = ("_redis",)
__slots__ = ("_redis", "handle_client_shutdown", "_get_and_renew_script", "_delete_all_script")
Copy link
Member

Choose a reason for hiding this comment

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

probably a dumb question but why do we expose private methods here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that __slots__ isn't about exposing things like __all__ is. __slots__ merely tells Python to only allow certain attributes on the class in order to optimize memory. Private attributes are no exception.

Copy link
Member

@JacobCoffee JacobCoffee Mar 28, 2024

Choose a reason for hiding this comment

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

ahh yep.. i thought this was __all__ i think i got lost in the buzz.

"warn_pdb_on_exception",
"warn_sync_to_thread_with_async_callable",
"warn_sync_to_thread_with_generator",
)
Copy link
Member

Choose a reason for hiding this comment

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

idk if we want these exposed in documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to leave the flake8 dunder additions unmodified so those more experienced with the library can decide what to exclude 👍

@peterschutt
Copy link
Contributor

I made a PR to see if we can get f8-d-a to play nice with the type check imports - don't really know what I'm doing though, so lets see what happens: python-formate/flake8-dunder-all#65

@peterschutt
Copy link
Contributor

I made a PR to see if we can get f8-d-a to play nice with the type check imports - don't really know what I'm doing though, so lets see what happens: python-formate/flake8-dunder-all#65

They released f8-d-a 0.4 which has support for an if TYPE_CHECKING block however there's an issue with how it handles if TYPE_CHECKING blocks that have stuff other than simple imports in it which I've opened an issue for. Either way this PR will be superseded, or we change the way we use the tool, or stop using it altogether.

WRT changing how we use the tool, it might be better to use it as a lint without the auto-fix, so it forces us to have the __all__ defined, but not populate it for us.

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.

Bug: slotscheck pre-commit misconfiguration
3 participants