Skip to content

Commit

Permalink
Use PEP-604 syntax for unions (#185)
Browse files Browse the repository at this point in the history
Activate the ruff lint rule "UP007" that updates `typing.Union` and `typing.Optional` to use the ` | ` syntax. I also made some manual fixups for the cases that were not updated by ruff.

This is a continuation of #184
  • Loading branch information
hamdanal authored Aug 27, 2023
1 parent e74795b commit 31260e4
Show file tree
Hide file tree
Showing 293 changed files with 3,561 additions and 3,865 deletions.
10 changes: 5 additions & 5 deletions django-stubs/apps/config.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterator
from typing import Any, Optional
from typing import Any

from django.apps.registry import Apps
from django.db.models.base import Model
Expand All @@ -8,14 +8,14 @@ MODELS_MODULE_NAME: str

class AppConfig:
name: str = ...
module: Optional[Any] = ...
apps: Optional[Apps] = ...
module: Any | None = ...
apps: Apps | None = ...
label: str = ...
verbose_name: str = ...
path: str = ...
models_module: Optional[str] = ...
models_module: str | None = ...
models: dict[str, type[Model]] = ...
def __init__(self, app_name: str, app_module: Optional[Any]) -> None: ...
def __init__(self, app_name: str, app_module: Any | None) -> None: ...
@classmethod
def create(cls, entry: str) -> AppConfig: ...
def get_model(self, model_name: str, require_ready: bool = ...) -> type[Model]: ...
Expand Down
14 changes: 6 additions & 8 deletions django-stubs/apps/registry.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import threading
from collections.abc import Callable, Iterable
from typing import Any, Optional, Union
from typing import Any

from django.db.models.base import Model

Expand All @@ -17,11 +17,9 @@ class Apps:
models_ready: bool = ...
ready: bool = ...
def __init__(
self, installed_apps: Optional[Iterable[Union[AppConfig, str]]] = ...
) -> None: ...
def populate(
self, installed_apps: Iterable[Union[AppConfig, str]] = ...
self, installed_apps: Iterable[AppConfig | str] | None = ...
) -> None: ...
def populate(self, installed_apps: Iterable[AppConfig | str] = ...) -> None: ...
def check_apps_ready(self) -> None: ...
def check_models_ready(self) -> None: ...
def get_app_configs(self) -> Iterable[AppConfig]: ...
Expand All @@ -31,13 +29,13 @@ class Apps:
self, include_auto_created: bool = ..., include_swapped: bool = ...
) -> list[type[Model]]: ...
def get_model(
self, app_label: str, model_name: Optional[str] = ..., require_ready: bool = ...
self, app_label: str, model_name: str | None = ..., require_ready: bool = ...
) -> type[Model]: ...
def register_model(self, app_label: str, model: type[Model]) -> None: ...
def is_installed(self, app_name: str) -> bool: ...
def get_containing_app_config(self, object_name: str) -> Optional[AppConfig]: ...
def get_containing_app_config(self, object_name: str) -> AppConfig | None: ...
def get_registered_model(self, app_label: str, model_name: str) -> type[Model]: ...
def get_swappable_settings_name(self, to_string: str) -> Optional[str]: ...
def get_swappable_settings_name(self, to_string: str) -> str | None: ...
def set_available_apps(self, available: Iterable[str]) -> None: ...
def unset_available_apps(self) -> None: ...
def set_installed_apps(self, installed: Iterable[str]) -> None: ...
Expand Down
36 changes: 18 additions & 18 deletions django-stubs/conf/global_settings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from re import Pattern

# This is defined here as a do-nothing function because we can't import
# django.utils.translation -- that module depends on the settings.
from typing import Any, Optional, Protocol, Union
from typing import Any, Protocol

####################
# CORE #
Expand Down Expand Up @@ -59,8 +59,8 @@ LOCALE_PATHS: list[str] = ...

# Settings for language cookie
LANGUAGE_COOKIE_NAME: str = ...
LANGUAGE_COOKIE_AGE: Optional[int] = ...
LANGUAGE_COOKIE_DOMAIN: Optional[str] = ...
LANGUAGE_COOKIE_AGE: int | None = ...
LANGUAGE_COOKIE_DOMAIN: str | None = ...
LANGUAGE_COOKIE_PATH: str = ...

# If you set this to True, Django will format dates, numbers and calendars
Expand Down Expand Up @@ -90,7 +90,7 @@ DATABASES: dict[str, dict[str, Any]] = ...
class Router(Protocol):
def allow_migrate(self, db: Any, app_label: Any, **hints: Any) -> Any: ...

DATABASE_ROUTERS: list[Union[str, Router]] = ...
DATABASE_ROUTERS: list[str | Router] = ...

# The email backend to use. For possible shortcuts see django.core.mail.
# The default is to use the SMTP backend.
Expand All @@ -112,9 +112,9 @@ EMAIL_HOST_USER: str = ...
EMAIL_HOST_PASSWORD: str = ...
EMAIL_USE_TLS: bool = ...
EMAIL_USE_SSL: bool = ...
EMAIL_SSL_CERTFILE: Optional[str] = ...
EMAIL_SSL_KEYFILE: Optional[str] = ...
EMAIL_TIMEOUT: Optional[int] = ...
EMAIL_SSL_CERTFILE: str | None = ...
EMAIL_SSL_KEYFILE: str | None = ...
EMAIL_TIMEOUT: int | None = ...

# List of strings representing installed apps.
INSTALLED_APPS: list[str] = ...
Expand Down Expand Up @@ -185,11 +185,11 @@ MEDIA_URL: str = ...

# Absolute path to the directory static files should be collected to.
# Example: "/var/www/example.com/static/"
STATIC_ROOT: Optional[str] = ...
STATIC_ROOT: str | None = ...

# URL that handles the static files served from STATIC_ROOT.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL: Optional[str] = ...
STATIC_URL: str | None = ...

# List of upload handler classes to be applied in order.
FILE_UPLOAD_HANDLERS: list[str] = ...
Expand All @@ -209,7 +209,7 @@ DATA_UPLOAD_MAX_NUMBER_FIELDS: int = ...
# Directory in which upload streamed files will be temporarily saved. A value of
# `None` will make Django use the operating system's default temporary directory
# (i.e. "/tmp" on *nix systems).
FILE_UPLOAD_TEMP_DIR: Optional[str] = ...
FILE_UPLOAD_TEMP_DIR: str | None = ...

# The numeric mode to set newly-uploaded files to. The value should be a mode
# you'd pass directly to os.chmod; see https://docs.python.org/library/os.html#files-and-directories.
Expand All @@ -224,7 +224,7 @@ FILE_UPLOAD_DIRECTORY_PERMISSIONS = None
# The directory where this setting is pointing should contain subdirectories
# named as the locales, containing a formats.py file
# (i.e. "myproject.locale" for myproject/locale/en/formats.py etc. use)
FORMAT_MODULE_PATH: Optional[str] = ...
FORMAT_MODULE_PATH: str | None = ...

# Default formatting for date objects. See all available format strings here:
# https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
Expand Down Expand Up @@ -308,7 +308,7 @@ USE_X_FORWARDED_PORT: bool = ...
# 'django.core.wsgi.get_wsgi_application' is used, thus preserving the same
# behavior as previous versions of Django. Otherwise this should point to an
# actual WSGI application object.
WSGI_APPLICATION: Optional[str] = ...
WSGI_APPLICATION: str | None = ...

# If your Django app is behind a proxy that sets a header to specify secure
# connections, AND that proxy ensures that user-submitted headers with the
Expand All @@ -317,7 +317,7 @@ WSGI_APPLICATION: Optional[str] = ...
# that header/value, request.is_secure() will return True.
# WARNING! Only set this if you fully understand what you're doing. Otherwise,
# you may be opening yourself up to a security risk.
SECURE_PROXY_SSL_HEADER: Optional[tuple[str, str]] = ...
SECURE_PROXY_SSL_HEADER: tuple[str, str] | None = ...

##############
# MIDDLEWARE #
Expand All @@ -339,7 +339,7 @@ SESSION_COOKIE_NAME = "sessionid"
# Age of cookie, in seconds (default: 2 weeks).
SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 2
# A string like "example.com", or None for standard domain cookie.
SESSION_COOKIE_DOMAIN: Optional[str] = ...
SESSION_COOKIE_DOMAIN: str | None = ...
# Whether the session cookie should be secure (https:// only).
SESSION_COOKIE_SECURE = False
# The path of the session cookie.
Expand All @@ -348,7 +348,7 @@ SESSION_COOKIE_PATH = "/"
SESSION_COOKIE_HTTPONLY = True
# Whether to set the flag restricting cookie leaks on cross-site requests.
# This can be 'Lax', 'Strict', or None to disable the flag.
SESSION_COOKIE_SAMESITE: Optional[str] = ...
SESSION_COOKIE_SAMESITE: str | None = ...
# Whether to save the session data on every request.
SESSION_SAVE_EVERY_REQUEST = False
# Whether a user's session cookie expires when the Web browser is closed.
Expand All @@ -357,7 +357,7 @@ SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_ENGINE = "django.contrib.sessions.backends.db"
# Directory to store session files if using the file session module. If None,
# the backend will use a sensible default.
SESSION_FILE_PATH: Optional[str] = ...
SESSION_FILE_PATH: str | None = ...
# class to serialize session data
SESSION_SERIALIZER = "django.contrib.sessions.serializers.JSONSerializer"

Expand All @@ -383,7 +383,7 @@ LOGIN_URL = "/accounts/login/"

LOGIN_REDIRECT_URL: str = ...

LOGOUT_REDIRECT_URL: Optional[str] = ...
LOGOUT_REDIRECT_URL: str | None = ...

# The number of days a password reset link is valid for
PASSWORD_RESET_TIMEOUT_DAYS = 3
Expand Down Expand Up @@ -416,7 +416,7 @@ CSRF_COOKIE_DOMAIN = None
CSRF_COOKIE_PATH = "/"
CSRF_COOKIE_SECURE = False
CSRF_COOKIE_HTTPONLY = False
CSRF_COOKIE_SAMESITE: Optional[str] = ...
CSRF_COOKIE_SAMESITE: str | None = ...
CSRF_HEADER_NAME = "HTTP_X_CSRFTOKEN"
CSRF_TRUSTED_ORIGINS: list[str] = ...
CSRF_USE_SESSIONS = False
Expand Down
14 changes: 7 additions & 7 deletions django-stubs/conf/urls/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Stubs for django.conf.urls (Python 3.5)
from collections.abc import Callable
from typing import Any, Optional, Union, overload
from typing import Any, overload

from django.http.response import HttpResponse, HttpResponseBase
from django.urls import URLPattern, URLResolver

handler400: Union[str, Callable[..., HttpResponse]] = ...
handler403: Union[str, Callable[..., HttpResponse]] = ...
handler404: Union[str, Callable[..., HttpResponse]] = ...
handler500: Union[str, Callable[..., HttpResponse]] = ...
handler400: str | Callable[..., HttpResponse] = ...
handler403: str | Callable[..., HttpResponse] = ...
handler404: str | Callable[..., HttpResponse] = ...
handler500: str | Callable[..., HttpResponse] = ...

IncludedURLConf = tuple[list[URLResolver], Optional[str], Optional[str]]
IncludedURLConf = tuple[list[URLResolver], str | None, str | None]

def include(arg: Any, namespace: str = ..., app_name: str = ...) -> IncludedURLConf: ...
@overload
Expand All @@ -27,7 +27,7 @@ def url(
@overload
def url(
regex: str,
view: list[Union[URLResolver, str]],
view: list[URLResolver | str],
kwargs: dict[str, Any] = ...,
name: str = ...,
) -> URLResolver: ...
4 changes: 2 additions & 2 deletions django-stubs/contrib/admin/actions.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any

from django.contrib.admin.options import ModelAdmin
from django.core.handlers.wsgi import WSGIRequest
Expand All @@ -7,4 +7,4 @@ from django.template.response import TemplateResponse

def delete_selected(
modeladmin: ModelAdmin[Any], request: WSGIRequest, queryset: QuerySet[Any]
) -> Optional[TemplateResponse]: ...
) -> TemplateResponse | None: ...
6 changes: 3 additions & 3 deletions django-stubs/contrib/admin/checks.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from collections.abc import Sequence
from typing import Any, Optional, Union
from typing import Any

from django.apps.config import AppConfig
from django.contrib.admin.options import BaseModelAdmin
from django.core.checks.messages import CheckMessage, Error

_CheckError = Union[str, Error]
_CheckError = str | Error

def check_admin_app(
app_configs: Optional[Sequence[AppConfig]] = ..., **kwargs: Any
app_configs: Sequence[AppConfig] | None = ..., **kwargs: Any
) -> list[_CheckError]: ...
def check_dependencies(**kwargs: Any) -> list[_CheckError]: ...

Expand Down
25 changes: 13 additions & 12 deletions django-stubs/contrib/admin/decorators.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Sequence
from typing import Any, Optional, TypeVar, Union
from typing import Any, TypeVar

from django.contrib.admin import ModelAdmin
from django.db.models import Combinable, QuerySet
Expand All @@ -10,19 +10,20 @@ from django.http import HttpRequest, HttpResponse
_M = TypeVar("_M", bound=Model)

def action(
function: Optional[
Callable[[ModelAdmin[_M], HttpRequest, QuerySet[_M]], Optional[HttpResponse]]
] = ...,
function: (
Callable[[ModelAdmin[_M], HttpRequest, QuerySet[_M]], HttpResponse | None]
| None
) = ...,
*,
permissions: Optional[Sequence[str]] = ...,
description: Optional[str] = ...,
permissions: Sequence[str] | None = ...,
description: str | None = ...,
) -> Callable[..., Any]: ...
def display(
function: Optional[Callable[[_M], Any]] = ...,
function: Callable[[_M], Any] | None = ...,
*,
boolean: Optional[bool] = ...,
ordering: Optional[Union[str, Combinable, BaseExpression]] = ...,
description: Optional[str] = ...,
empty_value: Optional[str] = ...,
boolean: bool | None = ...,
ordering: str | Combinable | BaseExpression | None = ...,
description: str | None = ...,
empty_value: str | None = ...,
) -> Callable[..., Any]: ...
def register(*models: type[Model], site: Optional[Any] = ...) -> Callable[..., Any]: ...
def register(*models: type[Model], site: Any | None = ...) -> Callable[..., Any]: ...
10 changes: 5 additions & 5 deletions django-stubs/contrib/admin/filters.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Iterator
from typing import Any, Optional
from typing import Any

from django.contrib.admin.options import ModelAdmin
from django.core.handlers.wsgi import WSGIRequest
Expand All @@ -20,16 +20,16 @@ class ListFilter:
model_admin: ModelAdmin[Any],
) -> None: ...
def has_output(self) -> bool: ...
def choices(self, changelist: Any) -> Optional[Iterator[dict[str, Any]]]: ...
def choices(self, changelist: Any) -> Iterator[dict[str, Any]] | None: ...
def queryset(
self, request: Any, queryset: QuerySet[Any]
) -> Optional[QuerySet[Any]]: ...
def expected_parameters(self) -> Optional[list[str]]: ...
) -> QuerySet[Any] | None: ...
def expected_parameters(self) -> list[str] | None: ...

class SimpleListFilter(ListFilter):
parameter_name: Any = ...
lookup_choices: Any = ...
def value(self) -> Optional[str]: ...
def value(self) -> str | None: ...
def lookups(self, request: Any, model_admin: Any) -> list[tuple[Any, str]]: ...

class FieldListFilter(ListFilter):
Expand Down
Loading

0 comments on commit 31260e4

Please sign in to comment.