From 403f1fad6db13a3053046340789163ececcf754c Mon Sep 17 00:00:00 2001 From: Dan Redding <125183946+dangotbanned@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:11:02 +0100 Subject: [PATCH] feat(typing): Ban `typing.Optional` import using `ruff` (#3460) * feat(typing): Ban `typing.Optional` import using `ruff` The user will see this message ``` altair\utils\data.py:17:5: TID251 `typing.Optional` is banned: Use `Union[T, None]` instead. `typing.Optional` is likely to be confused with `altair.Optional`, which have similar but different semantic meaning. See https://github.com/vega/altair/pull/3449 Found 1 error. ``` Partial fix for https://github.com/vega/altair/pull/3452#discussion_r1663857513 * fix: typo in message --- altair/utils/data.py | 3 +-- pyproject.toml | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/altair/utils/data.py b/altair/utils/data.py index 8ab46a6f7..61923231c 100644 --- a/altair/utils/data.py +++ b/altair/utils/data.py @@ -14,7 +14,6 @@ TypeVar, Union, Dict, - Optional, overload, runtime_checkable, ) @@ -54,7 +53,7 @@ class SupportsGeoInterface(Protocol): str, Union[str, Dict[Any, Any], List[Dict[Any, Any]]] ] ToValuesReturnType: TypeAlias = Dict[str, Union[Dict[Any, Any], List[Dict[Any, Any]]]] -SampleReturnType = Optional[Union[pd.DataFrame, Dict[str, Sequence], "pa.lib.Table"]] +SampleReturnType = Union[pd.DataFrame, Dict[str, Sequence], "pa.lib.Table", None] def is_data_type(obj: Any) -> TypeIs[DataType]: diff --git a/pyproject.toml b/pyproject.toml index 931c34581..3ddaa278b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -319,6 +319,10 @@ unfixable= [ [tool.ruff.lint.mccabe] max-complexity = 18 +[tool.ruff.lint.flake8-tidy-imports.banned-api] +# https://docs.astral.sh/ruff/settings/#lint_flake8-tidy-imports_banned-api +"typing.Optional".msg = "Use `Union[T, None]` instead.\n`typing.Optional` is likely to be confused with `altair.Optional`, which have a similar but different semantic meaning.\nSee https://github.com/vega/altair/pull/3449" + [tool.ruff.format] quote-style = "double" indent-style = "space"