Skip to content

Commit

Permalink
Export test fixes (#2887)
Browse files Browse the repository at this point in the history
* Remove incorrect branch

* Stop supporting jedi on PyPy

* Spotted pyright fixes

* Fix formatting

* Fix mypy test

* Fix pyright too

* Remove finished TODO
  • Loading branch information
A5rocks authored Nov 28, 2023
1 parent 071d636 commit 59731c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
23 changes: 5 additions & 18 deletions src/trio/_tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/trio/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@
ntohs as ntohs,
)

if sys.implementation.name == "cpython":

This comment has been minimized.

Copy link
@Cheaterman

Cheaterman Jan 2, 2024

This block is no longer under _suppress(ImportError) but these functions aren't available on Android with API < 24 (and possibly other platforms). Would it be OK to re-add the with _suppress(ImportError) bit?

This comment has been minimized.

Copy link
@jakkdl

jakkdl Jan 3, 2024

Member

Seems fine, open a PR/issue.

EDIT: see #2915

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,
)

Expand Down

0 comments on commit 59731c3

Please sign in to comment.