Skip to content

use to lower-case list and dict, this time with tests #6161

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

Merged
merged 5 commits into from
Oct 13, 2021
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: ./tests/check_pep_604.py
- run: ./tests/check_new_syntax.py

flake8:
name: Lint with flake8
Expand Down
8 changes: 4 additions & 4 deletions stdlib/multiprocessing/pool.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from _typeshed import Self
from typing import Any, Callable, ContextManager, Dict, Generic, Iterable, Iterator, List, Mapping, TypeVar
from typing import Any, Callable, ContextManager, Generic, Iterable, Iterator, List, Mapping, TypeVar

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -17,7 +17,7 @@ class ApplyResult(Generic[_T]):
else:
def __init__(
self,
cache: Dict[int, ApplyResult[Any]],
cache: dict[int, ApplyResult[Any]],
callback: Callable[[_T], None] | None,
error_callback: Callable[[BaseException], None] | None,
) -> None: ...
Expand All @@ -44,7 +44,7 @@ class MapResult(ApplyResult[List[_T]]):
else:
def __init__(
self,
cache: Dict[int, ApplyResult[Any]],
cache: dict[int, ApplyResult[Any]],
chunksize: int,
length: int,
callback: Callable[[List[_T]], None] | None,
Expand All @@ -55,7 +55,7 @@ class IMapIterator(Iterator[_T]):
if sys.version_info >= (3, 8):
def __init__(self, pool: Pool) -> None: ...
else:
def __init__(self, cache: Dict[int, IMapIterator[Any]]) -> None: ...
def __init__(self, cache: dict[int, IMapIterator[Any]]) -> None: ...
def __iter__(self: _S) -> _S: ...
def next(self, timeout: float | None = ...) -> _T: ...
def __next__(self, timeout: float | None = ...) -> _T: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/plistlib.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from datetime import datetime
from enum import Enum
from typing import IO, Any, Dict as _Dict, List, Mapping, MutableMapping, Tuple, Type
from typing import IO, Any, Dict as _Dict, Mapping, MutableMapping, Tuple, Type

class PlistFormat(Enum):
FMT_XML: int
Expand Down Expand Up @@ -31,15 +31,15 @@ else:
) -> Any: ...

def dump(
value: Mapping[str, Any] | List[Any] | Tuple[Any, ...] | str | bool | float | bytes | datetime,
value: Mapping[str, Any] | list[Any] | Tuple[Any, ...] | str | bool | float | bytes | datetime,
fp: IO[bytes],
*,
fmt: PlistFormat = ...,
sort_keys: bool = ...,
skipkeys: bool = ...,
) -> None: ...
def dumps(
value: Mapping[str, Any] | List[Any] | Tuple[Any, ...] | str | bool | float | bytes | datetime,
value: Mapping[str, Any] | list[Any] | Tuple[Any, ...] | str | bool | float | bytes | datetime,
*,
fmt: PlistFormat = ...,
skipkeys: bool = ...,
Expand Down
2 changes: 1 addition & 1 deletion stubs/beautifulsoup4/bs4/element.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Tag(PageElement):
sourcepos: int | None
known_xml: bool | None
attrs: Mapping[str, str]
contents: List[PageElement]
contents: list[PageElement]
hidden: bool
can_be_empty_element: bool | None
cdata_list_attributes: list[str] | None
Expand Down
12 changes: 6 additions & 6 deletions stubs/dateparser/dateparser/languages/loader.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from typing import Any, Iterator, List
from typing import Any, Iterator

from .locale import Locale

Expand All @@ -8,16 +8,16 @@ LOCALE_SPLIT_PATTERN: Any
class LocaleDataLoader:
def get_locale_map(
self,
languages: List[str] | None = ...,
locales: List[str] | None = ...,
languages: list[str] | None = ...,
locales: list[str] | None = ...,
region: str | None = ...,
use_given_order: bool = ...,
allow_conflicting_locales: bool = ...,
) -> OrderedDict[str, List[Any] | str | int]: ...
) -> OrderedDict[str, list[Any] | str | int]: ...
def get_locales(
self,
languages: List[str] | None = ...,
locales: List[str] | None = ...,
languages: list[str] | None = ...,
locales: list[str] | None = ...,
region: str | None = ...,
use_given_order: bool = ...,
allow_conflicting_locales: bool = ...,
Expand Down
10 changes: 5 additions & 5 deletions stubs/dateparser/dateparser/search/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from datetime import datetime
from typing import Any, List, Mapping, Set, Tuple, overload
from typing import Any, Mapping, Set, Tuple, overload

if sys.version_info >= (3, 8):
from typing import Literal
Expand All @@ -10,14 +10,14 @@ else:
@overload
def search_dates(
text: str,
languages: List[str] | Tuple[str] | Set[str] | None,
languages: list[str] | Tuple[str] | Set[str] | None,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tuple[str] appears to be a typo. I'll fix it in a separate PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should just be Collection[str] or something

settings: Mapping[Any, Any] | None,
add_detected_language: Literal[True],
) -> List[Tuple[str, datetime, str]]: ...
) -> list[Tuple[str, datetime, str]]: ...
@overload
def search_dates(
text: str,
languages: List[str] | Tuple[str] | Set[str] | None = ...,
languages: list[str] | Tuple[str] | Set[str] | None = ...,
settings: Mapping[Any, Any] | None = ...,
add_detected_language: Literal[False] = ...,
) -> List[Tuple[str, datetime]]: ...
) -> list[Tuple[str, datetime]]: ...
6 changes: 3 additions & 3 deletions stubs/entrypoints/entrypoints.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from _typeshed import Self
from typing import Any, Dict, Iterator, List, Sequence, Text, Tuple, Type
from typing import Any, Iterator, Sequence, Text, Tuple, Type

if sys.version_info >= (3, 0):
from configparser import ConfigParser
Expand Down Expand Up @@ -53,5 +53,5 @@ def iter_files_distros(
path: Sequence[Text] | None = ..., repeated_distro: Text = ...
) -> Iterator[Tuple[ConfigParser, Distribution | None]]: ...
def get_single(group: Text, name: Text, path: Sequence[Text] | None = ...) -> EntryPoint: ...
def get_group_named(group: Text, path: Sequence[Text] | None = ...) -> Dict[str, EntryPoint]: ...
def get_group_all(group: Text, path: Sequence[Text] | None = ...) -> List[EntryPoint]: ...
def get_group_named(group: Text, path: Sequence[Text] | None = ...) -> dict[str, EntryPoint]: ...
def get_group_all(group: Text, path: Sequence[Text] | None = ...) -> list[EntryPoint]: ...
41 changes: 19 additions & 22 deletions tests/check_pep_604.py → tests/check_new_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,44 @@
from pathlib import Path


def check_pep_604(tree: ast.AST, path: Path) -> list[str]:
def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
errors = []

class UnionFinder(ast.NodeVisitor):
class OldSyntaxFinder(ast.NodeVisitor):
def visit_Subscript(self, node: ast.Subscript) -> None:
if (
isinstance(node.value, ast.Name)
and node.value.id == "Union"
and isinstance(node.slice, ast.Tuple)
):
if not isinstance(node.value, ast.Name):
return

if node.value.id == "Union" and isinstance(node.slice, ast.Tuple):
new_syntax = " | ".join(ast.unparse(x) for x in node.slice.elts)
errors.append(
(f"{path}:{node.lineno}: Use PEP 604 syntax for Union, e.g. `{new_syntax}`")
)
if (
isinstance(node.value, ast.Name)
and node.value.id == "Optional"
):
errors.append(f"{path}:{node.lineno}: Use PEP 604 syntax for Union, e.g. `{new_syntax}`")
if node.value.id == "Optional":
new_syntax = f"{ast.unparse(node.slice)} | None"
errors.append(
(f"{path}:{node.lineno}: Use PEP 604 syntax for Optional, e.g. `{new_syntax}`")
)
errors.append(f"{path}:{node.lineno}: Use PEP 604 syntax for Optional, e.g. `{new_syntax}`")
if node.value.id in {"List", "Dict"}:
new_syntax = f"{node.value.id.lower()}[{ast.unparse(node.slice)}]"
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")

# This doesn't check type aliases (or type var bounds, etc), since those are not
# currently supported
#
# TODO: can use built-in generics in type aliases
class AnnotationFinder(ast.NodeVisitor):
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
UnionFinder().visit(node.annotation)
OldSyntaxFinder().visit(node.annotation)

def visit_arg(self, node: ast.arg) -> None:
if node.annotation is not None:
UnionFinder().visit(node.annotation)
OldSyntaxFinder().visit(node.annotation)

def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
if node.returns is not None:
UnionFinder().visit(node.returns)
OldSyntaxFinder().visit(node.returns)
self.generic_visit(node)

def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
if node.returns is not None:
UnionFinder().visit(node.returns)
OldSyntaxFinder().visit(node.returns)
self.generic_visit(node)

AnnotationFinder().visit(tree)
Expand All @@ -63,7 +60,7 @@ def main() -> None:

with open(path) as f:
tree = ast.parse(f.read())
errors.extend(check_pep_604(tree, path))
errors.extend(check_new_syntax(tree, path))

if errors:
print("\n".join(errors))
Expand Down