Skip to content

Commit

Permalink
PEP 563 + typing.NamedTuple x 9.
Browse files Browse the repository at this point in the history
This commit is the next in a commit chain explicitly supporting
`typing.NamedTuple` subclasses under PEP 563 (i.e., `from __future__
import annotations`), en-route to resolving issue #318 kindly submitted
by the cosmically rare-earth GitHub element @kasium. For unknown
reasons, `typing.NamedTuple` subclasses encapsulate type hints
stringified by PEP 563 as `typing.ForwardRef(...)` objects -- which is
just all manner of strange. In response, this commit heavily refactors
the beartype codebase to globally replace *almost* all remaining usage
of the antiquated beartypistry with proper forward reference proxies.
Doing so significantly improves the maintainability and debuggability of
forward reference handling throughout beartype. This is important,
because it preserves @leycec's sanity in the short term. (*Clueless blue lessons!*)
  • Loading branch information
leycec committed Feb 7, 2024
1 parent bebad05 commit a7794bb
Show file tree
Hide file tree
Showing 21 changed files with 729 additions and 898 deletions.
4 changes: 2 additions & 2 deletions beartype/_check/checkmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
)
from beartype._util.func.utilfuncmake import make_func
from beartype._util.hint.pep.proposal.pep484585.utilpep484585ref import (
get_hint_pep484585_ref_name_absolute)
get_hint_pep484585_ref_names_relative_to)
from beartype._util.hint.utilhinttest import is_hint_ignorable
from itertools import count
from warnings import warn
Expand Down Expand Up @@ -648,7 +648,7 @@ def _make_func_checker(
# type-checkers) sufficiently slow as to be pragmatically infeasible.
if hint_refs_type_basename:
# Defer to a low-level getter to raise a human-readable exception.
get_hint_pep484585_ref_name_absolute(
get_hint_pep484585_ref_names_relative_to(
# First relative forward reference in this type hint,
# arbitrarily chosen for convenience.
hint=hint_refs_type_basename[0],
Expand Down
5 changes: 2 additions & 3 deletions beartype/_check/code/codemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
HINT_META_INDEX_PITH_VAR_NAME,
HINT_META_INDEX_INDENT,
)
from beartype._check.code._codescope import (
from beartype._check.code.codescope import (
add_func_scope_type,
add_func_scope_types,
add_func_scope_type_or_types,
Expand Down Expand Up @@ -843,8 +843,7 @@ def _enqueue_hint_child(pith_child_expr: str) -> str:
hint_curr_expr, hint_refs_type_basename = (
express_func_scope_type_ref(
forwardref=hint_curr,
forwardrefs_class_basename=(
hint_refs_type_basename),
forwardrefs_class_basename=hint_refs_type_basename,
func_scope=func_wrapper_scope,
exception_prefix=_EXCEPTION_PREFIX,
))
Expand Down

0 comments on commit a7794bb

Please sign in to comment.