From 2b702233c62f678e03ad06757f77a5f77206a9a6 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 28 Nov 2021 18:04:46 +0200 Subject: [PATCH] tests/check_new_syntax.py: check order of if statements (#6423) --- stdlib/asyncio/coroutines.pyi | 6 +++--- stdlib/inspect.pyi | 16 ++++++++-------- stdlib/ssl.pyi | 6 +++--- tests/check_new_syntax.py | 11 +++++++++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/stdlib/asyncio/coroutines.pyi b/stdlib/asyncio/coroutines.pyi index b321ece76..6c2d8179d 100644 --- a/stdlib/asyncio/coroutines.pyi +++ b/stdlib/asyncio/coroutines.pyi @@ -13,8 +13,8 @@ if sys.version_info < (3, 11): def iscoroutinefunction(func: object) -> bool: ... -if sys.version_info < (3, 8): - def iscoroutine(obj: object) -> TypeGuard[types.GeneratorType[Any, Any, Any] | Coroutine[Any, Any, Any]]: ... +if sys.version_info >= (3, 8): + def iscoroutine(obj: object) -> TypeGuard[Coroutine[Any, Any, Any]]: ... else: - def iscoroutine(obj: object) -> TypeGuard[Coroutine[Any, Any, Any]]: ... + def iscoroutine(obj: object) -> TypeGuard[types.GeneratorType[Any, Any, Any] | Coroutine[Any, Any, Any]]: ... diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index 511944160..82a59d613 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -93,14 +93,7 @@ def isframe(object: object) -> TypeGuard[FrameType]: ... def iscode(object: object) -> TypeGuard[CodeType]: ... def isbuiltin(object: object) -> TypeGuard[BuiltinFunctionType]: ... -if sys.version_info < (3, 7): - def isroutine( - object: object, - ) -> TypeGuard[FunctionType | LambdaType | MethodType | BuiltinFunctionType | BuiltinMethodType]: ... - def ismethoddescriptor(object: object) -> bool: ... - def ismemberdescriptor(object: object) -> bool: ... - -else: +if sys.version_info >= (3, 7): def isroutine( object: object, ) -> TypeGuard[ @@ -116,6 +109,13 @@ else: def ismethoddescriptor(object: object) -> TypeGuard[MethodDescriptorType]: ... def ismemberdescriptor(object: object) -> TypeGuard[MemberDescriptorType]: ... +else: + def isroutine( + object: object, + ) -> TypeGuard[FunctionType | LambdaType | MethodType | BuiltinFunctionType | BuiltinMethodType]: ... + def ismethoddescriptor(object: object) -> bool: ... + def ismemberdescriptor(object: object) -> bool: ... + def isabstract(object: object) -> bool: ... def isgetsetdescriptor(object: object) -> TypeGuard[GetSetDescriptorType]: ... def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ... diff --git a/stdlib/ssl.pyi b/stdlib/ssl.pyi index 3969b8677..489327fd4 100644 --- a/stdlib/ssl.pyi +++ b/stdlib/ssl.pyi @@ -308,7 +308,9 @@ class SSLSocket(socket.socket): server_hostname: str | None session: SSLSession | None session_reused: bool | None - if sys.version_info < (3, 7): + if sys.version_info >= (3, 7): + def __init__(self, *args: Any, **kwargs: Any) -> None: ... + else: def __init__( self, sock: socket.socket | None = ..., @@ -330,8 +332,6 @@ class SSLSocket(socket.socket): _context: SSLContext | None = ..., _session: Any | None = ..., ) -> None: ... - else: - def __init__(self, *args: Any, **kwargs: Any) -> None: ... def connect(self, addr: socket._Address | bytes) -> None: ... def connect_ex(self, addr: socket._Address | bytes) -> int: ... def recv(self, buflen: int = ..., flags: int = ...) -> bytes: ... diff --git a/tests/check_new_syntax.py b/tests/check_new_syntax.py index f1bbd54f9..09efdac84 100755 --- a/tests/check_new_syntax.py +++ b/tests/check_new_syntax.py @@ -117,7 +117,18 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]: self.old_syntax_finder().visit(node.returns) self.generic_visit(node) + class IfFinder(ast.NodeVisitor): + def visit_If(self, node: ast.If) -> None: + if isinstance(node.test, ast.Compare) and ast.unparse(node.test).startswith("sys.version_info < ") and node.orelse: + new_syntax = "if " + ast.unparse(node.test).replace("<", ">=", 1) + errors.append( + f"{path}:{node.lineno}: When using if/else with sys.version_info, " + f"put the code for new Python versions first, e.g. `{new_syntax}`" + ) + self.generic_visit(node) + AnnotationFinder().visit(tree) + IfFinder().visit(tree) return errors