From 59731c31b0ee0441d9f561f31ac0fdcc6cb99072 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Mon, 27 Nov 2023 23:38:10 -0500 Subject: [PATCH] Export test fixes (#2887) * Remove incorrect branch * Stop supporting jedi on PyPy * Spotted pyright fixes * Fix formatting * Fix mypy test * Fix pyright too * Remove finished TODO --- src/trio/__init__.py | 5 ++++- src/trio/_tests/test_exports.py | 23 +++++------------------ src/trio/socket.py | 11 ++++++++--- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/trio/__init__.py b/src/trio/__init__.py index 5adf14618e..115211934c 100644 --- a/src/trio/__init__.py +++ b/src/trio/__init__.py @@ -2,6 +2,8 @@ """ from __future__ import annotations +from typing import TYPE_CHECKING + # General layout: # # trio/_core/... is the self-contained core library. It does various @@ -111,7 +113,7 @@ # Not imported by default, but mentioned here so static analysis tools like # pylint will know that it exists. -if False: +if TYPE_CHECKING: from . import testing from . import _deprecate as _deprecate @@ -154,3 +156,4 @@ fixup_module_metadata(from_thread.__name__, from_thread.__dict__) fixup_module_metadata(to_thread.__name__, to_thread.__dict__) del fixup_module_metadata +del TYPE_CHECKING diff --git a/src/trio/_tests/test_exports.py b/src/trio/_tests/test_exports.py index 8afb710eb6..c77a08f020 100644 --- a/src/trio/_tests/test_exports.py +++ b/src/trio/_tests/test_exports.py @@ -157,6 +157,9 @@ def no_underscores(symbols: Iterable[str]) -> set[str]: ast = linter.get_ast(module.__file__, modname) static_names = no_underscores(ast) # type: ignore[arg-type] elif tool == "jedi": + if sys.implementation.name != "cpython": + pytest.skip("jedi does not support pypy") + try: import jedi except ImportError as error: @@ -195,7 +198,8 @@ def no_underscores(symbols: Iterable[str]) -> set[str]: ) elif tool == "pyright_verifytypes": if not RUN_SLOW: # pragma: no cover - pytest.skip("use --run-slow to check against mypy") + pytest.skip("use --run-slow to check against pyright") + try: import pyright # noqa: F401 except ImportError as error: @@ -213,26 +217,9 @@ def no_underscores(symbols: Iterable[str]) -> set[str]: for x in current_result["typeCompleteness"]["symbols"] if x["name"].startswith(modname) } - - # pyright ignores the symbol defined behind `if False` - if modname == "trio": - static_names.add("testing") - - # these are hidden behind `if sys.platform != "win32" or not TYPE_CHECKING` - # so presumably pyright is parsing that if statement, in which case we don't - # care about them being missing. - if modname == "trio.socket" and sys.platform == "win32": - ignored_missing_names = {"if_indextoname", "if_nameindex", "if_nametoindex"} - assert static_names.isdisjoint(ignored_missing_names) - static_names.update(ignored_missing_names) - else: # pragma: no cover raise AssertionError() - # mypy handles errors with an `assert` in its branch - if tool == "mypy": - return - # It's expected that the static set will contain more names than the # runtime set: # - static tools are sometimes sloppy and include deleted names diff --git a/src/trio/socket.py b/src/trio/socket.py index 3fd9e3ce91..8e2e24aafd 100644 --- a/src/trio/socket.py +++ b/src/trio/socket.py @@ -67,13 +67,18 @@ ntohs as ntohs, ) +if sys.implementation.name == "cpython": + from socket import ( + if_indextoname as if_indextoname, + if_nameindex as if_nameindex, + if_nametoindex as if_nametoindex, + ) + + # not always available so expose only if if sys.platform != "win32" or not _t.TYPE_CHECKING: with _suppress(ImportError): from socket import ( - if_indextoname as if_indextoname, - if_nameindex as if_nameindex, - if_nametoindex as if_nametoindex, sethostname as sethostname, )