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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,55 @@ repos:
rev: v0.3.1
hooks:
- id: ensure-dunder-all
exclude: "test*|examples*|tools"
# Need to be careful to not exclude too many files
# since "litestar" containts the word "test"
exclude: "test_|tests|examples|tools"
args: ["--use-tuple"]
- repo: https://github.com/ariebovenberg/slotscheck
rev: v0.19.0
hooks:
- id: slotscheck
exclude: "test_*|docs|.github"
# Need to be careful to not exclude too many files
# since "litestar" containts the word "test"
exclude: "test_|tests|docs|.github|^tools"
additional_dependencies:
- anyio>=3
- httpx>=0.22
- exceptiongroup
- importlib-metadata
- importlib-resources>=5.12.0
- msgspec>=0.18.2
- multidict>=6.0.2
- polyfactory>=2.6.3
- pyyaml
- typing-extensions
- click
- rich>=13.0.0
- rich-click
- annotated-types
- attrs
- brotli
- jsbeautifier
- uvicorn[standard]
- uvloop>=0.18.0; sys_platform != 'win32'
- cryptography
- jinja2>=3.1.2
- python-jose
- mako>=1.2.4
- minijinja>=1.0.0
- opentelemetry-instrumentation-asgi
- piccolo
- picologging
- prometheus-client
- pydantic
- email-validator
- pydantic-extra-types
- redis[hiredis]>=4.4.4
- advanced-alchemy>=0.2.2,<1.0.0
- structlog
- asyncpg
- psycopg
- docutils
- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: "v0.9.1"
hooks:
Expand Down
2 changes: 2 additions & 0 deletions litestar/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from litestar.cli.main import litestar_group

__all__ = ("run_cli",)


def run_cli() -> None:
"""Application Entrypoint."""
Expand Down
7 changes: 7 additions & 0 deletions litestar/_openapi/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from litestar.exceptions import ImproperlyConfiguredException
from litestar.openapi.spec import Reference, Schema

__all__ = (
"OpenAPIContext",
"RegisteredSchema",
"SchemaRegistry",
)


if TYPE_CHECKING:
from litestar.openapi import OpenAPIConfig
from litestar.plugins import OpenAPISchemaPluginProtocol
Expand Down
3 changes: 3 additions & 0 deletions litestar/_openapi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from litestar.plugins.base import ReceiveRoutePlugin
from litestar.routes import HTTPRoute

__all__ = ("OpenAPIPlugin",)


if TYPE_CHECKING:
from litestar.app import Litestar
from litestar.config.app import AppConfig
Expand Down
3 changes: 3 additions & 0 deletions litestar/_openapi/schema_generation/plugins/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from litestar.typing import FieldDefinition
from litestar.utils.predicates import is_optional_union

__all__ = ("DataclassSchemaPlugin",)


if TYPE_CHECKING:
from litestar._openapi.schema_generation import SchemaCreator
from litestar.openapi.spec import Schema
Expand Down
3 changes: 3 additions & 0 deletions litestar/_openapi/schema_generation/plugins/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from litestar.pagination import ClassicPagination, CursorPagination, OffsetPagination
from litestar.plugins import OpenAPISchemaPlugin

__all__ = ("PaginationSchemaPlugin",)


if TYPE_CHECKING:
from litestar._openapi.schema_generation import SchemaCreator
from litestar.typing import FieldDefinition
Expand Down
3 changes: 3 additions & 0 deletions litestar/_openapi/schema_generation/plugins/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from litestar.typing import FieldDefinition
from litestar.utils.predicates import is_optional_union

__all__ = ("StructSchemaPlugin",)


if TYPE_CHECKING:
from msgspec.structs import FieldInfo

Expand Down
3 changes: 3 additions & 0 deletions litestar/_openapi/schema_generation/plugins/typed_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from litestar.plugins import OpenAPISchemaPlugin
from litestar.typing import FieldDefinition

__all__ = ("TypedDictSchemaPlugin",)


if TYPE_CHECKING:
from litestar._openapi.schema_generation import SchemaCreator
from litestar.openapi.spec import Schema
Expand Down
8 changes: 8 additions & 0 deletions litestar/_openapi/schema_generation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
make_non_optional_union,
)

__all__ = (
"SchemaCreator",
"create_enum_schema",
"create_literal_schema",
"create_schema_for_annotation",
)


if TYPE_CHECKING:
from litestar._openapi.datastructures import OpenAPIContext
from litestar.openapi.spec import Example, Reference
Expand Down
2 changes: 2 additions & 0 deletions litestar/_signature/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from msgspec import ValidationError

__all__ = ("ExtendedMsgSpecValidationError",)


class ExtendedMsgSpecValidationError(ValidationError):
def __init__(self, errors: list[dict[str, Any]]) -> None:
Expand Down
4 changes: 0 additions & 4 deletions litestar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,17 @@ class Litestar(Router):
"csrf_config",
"event_emitter",
"get_logger",
"include_in_schema",
"logger",
"logging_config",
"multipart_form_part_limit",
"on_shutdown",
"on_startup",
"openapi_config",
"request_class",
"response_cache_config",
"route_map",
"signature_namespace",
"state",
"stores",
"template_engine",
"websocket_class",
"pdb_on_exception",
"experimental_features",
)
Expand Down
2 changes: 2 additions & 0 deletions litestar/channels/backends/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from litestar.channels import ChannelsBackend
from litestar.exceptions import ImproperlyConfiguredException

__all__ = ("AsyncPgChannelsBackend",)


class AsyncPgChannelsBackend(ChannelsBackend):
_listener_conn: asyncpg.Connection
Expand Down
2 changes: 2 additions & 0 deletions litestar/channels/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from abc import ABC, abstractmethod
from typing import AsyncGenerator, Iterable

__all__ = ("ChannelsBackend",)


class ChannelsBackend(ABC):
@abstractmethod
Expand Down
2 changes: 2 additions & 0 deletions litestar/channels/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from litestar.channels.backends.base import ChannelsBackend

__all__ = ("MemoryChannelsBackend",)


class MemoryChannelsBackend(ChannelsBackend):
"""An in-memory channels backend"""
Expand Down
2 changes: 2 additions & 0 deletions litestar/channels/backends/psycopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from .base import ChannelsBackend

__all__ = ("PsycoPgChannelsBackend",)


def _safe_quote(ident: str) -> str:
return '"{}"'.format(ident.replace('"', '""')) # sourcery skip
Expand Down
7 changes: 7 additions & 0 deletions litestar/channels/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

from litestar.channels.backends.base import ChannelsBackend

__all__ = (
"RedisChannelsBackend",
"RedisChannelsPubSubBackend",
"RedisChannelsStreamBackend",
)


if TYPE_CHECKING:
from redis.asyncio import Redis
from redis.asyncio.client import PubSub
Expand Down
6 changes: 6 additions & 0 deletions litestar/channels/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

from .subscriber import BacklogStrategy, EventCallback, Subscriber

__all__ = (
"ChannelsException",
"ChannelsPlugin",
)


if TYPE_CHECKING:
from types import TracebackType

Expand Down
6 changes: 6 additions & 0 deletions litestar/channels/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from contextlib import AsyncExitStack, asynccontextmanager, suppress
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generic, Literal, TypeVar

__all__ = (
"AsyncDeque",
"Subscriber",
)


Comment on lines +9 to +14
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

if TYPE_CHECKING:
from litestar.channels import ChannelsPlugin

Expand Down
3 changes: 3 additions & 0 deletions litestar/contrib/attrs/attrs_schema_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from litestar.typing import FieldDefinition
from litestar.utils import is_attrs_class, is_optional_union

__all__ = ("AttrsSchemaPlugin",)


try:
import attr
import attrs
Expand Down
2 changes: 0 additions & 2 deletions litestar/contrib/opentelemetry/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
class OpenTelemetryInstrumentationMiddleware(AbstractMiddleware):
"""OpenTelemetry Middleware."""

__slots__ = ("open_telemetry_middleware",)

def __init__(self, app: ASGIApp, config: OpenTelemetryConfig) -> None:
"""Middleware that adds OpenTelemetry instrumentation to the application.

Expand Down
2 changes: 2 additions & 0 deletions litestar/contrib/pydantic/pydantic_di_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from litestar.contrib.pydantic.utils import is_pydantic_model_class
from litestar.plugins import DIPlugin

__all__ = ("PydanticDIPlugin",)


class PydanticDIPlugin(DIPlugin):
def has_typed_init(self, type_: Any) -> bool:
Expand Down
8 changes: 8 additions & 0 deletions litestar/contrib/pydantic/pydantic_init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
from litestar.typing import _KWARG_META_EXTRACTORS
from litestar.utils import is_class_and_subclass

__all__ = (
"ConstrainedFieldMetaExtractor",
"PydanticInitPlugin",
"is_pydantic_v1_model_class",
"is_pydantic_v2_model_class",
)


try:
# check if we have pydantic v2 installed, and try to import both versions
import pydantic as pydantic_v2
Expand Down
3 changes: 3 additions & 0 deletions litestar/contrib/pydantic/pydantic_schema_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from litestar.typing import FieldDefinition
from litestar.utils import is_class_and_subclass, is_generic

__all__ = ("PydanticSchemaPlugin",)


try:
# check if we have pydantic v2 installed, and try to import both versions
import pydantic as pydantic_v2
Expand Down
13 changes: 13 additions & 0 deletions litestar/contrib/pydantic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
normalize_type_annotation,
)

__all__ = (
"create_field_definitions_for_computed_fields",
"is_pydantic_2_model",
"is_pydantic_constrained_field",
"is_pydantic_model_class",
"is_pydantic_model_instance",
"is_pydantic_undefined",
"pydantic_get_type_hints_with_generics_resolved",
"pydantic_get_unwrapped_annotation_and_type_hints",
"pydantic_unwrap_and_get_origin",
)


# isort: off
try:
from pydantic import v1 as pydantic_v1
Expand Down
3 changes: 3 additions & 0 deletions litestar/contrib/sqlalchemy/plugins/init/config/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from litestar.utils.deprecation import deprecated

__all__ = ("HasGetEngine",)


if TYPE_CHECKING:
from sqlalchemy import Engine
from sqlalchemy.ext.asyncio import AsyncEngine
Expand Down
16 changes: 13 additions & 3 deletions litestar/dto/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

from litestar.dto.data_structures import DTOFieldDefinition

__all__ = (
"CollectionType",
"CompositeType",
"MappingType",
"NestedFieldInfo",
"SimpleType",
"TransferDTOFieldDefinition",
"TransferType",
"TupleType",
"UnionType",
)


if TYPE_CHECKING:
from typing import Any

Expand Down Expand Up @@ -96,9 +109,6 @@ class MappingType(CompositeType):
@dataclass(frozen=True)
class TransferDTOFieldDefinition(DTOFieldDefinition):
__slots__ = (
"default_factory",
"dto_field",
"model_name",
"is_excluded",
"is_partial",
"serialization_name",
Expand Down
6 changes: 6 additions & 0 deletions litestar/dto/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

from litestar.typing import FieldDefinition

__all__ = (
"DTOData",
"DTOFieldDefinition",
)


if TYPE_CHECKING:
from typing import Any, Callable

Expand Down
9 changes: 9 additions & 0 deletions litestar/handlers/websocket_handlers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
from litestar.utils import ensure_async_callable
from litestar.utils.helpers import unwrap_partial

__all__ = (
"ListenerHandler",
"create_handle_receive",
"create_handle_send",
"create_handler_signature",
"create_stub_dependency",
)


if TYPE_CHECKING:
from litestar import WebSocket
from litestar.handlers.websocket_handlers.listener import WebsocketListenerRouteHandler
Expand Down
Loading
Loading