Skip to content

Commit

Permalink
Obscure code generation assertion squelched.
Browse files Browse the repository at this point in the history
This commit squelches (i.e., silences) a low-level `assert` statement
erroneously performed during @beartype's dynamic code generation loop,
resolving issue #352 kindly submitted by NES Zelda master @rbroderi.
(*Sworn to dastardly Master Swords!*)
  • Loading branch information
leycec committed Apr 3, 2024
1 parent a458668 commit de3f94d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
46 changes: 30 additions & 16 deletions beartype/_check/code/codemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,20 @@ def _enqueue_hint_child(pith_child_expr: str) -> str:
# Allow these local variables of the outer scope to be modified below.
nonlocal hints_meta_index_last

#FIXME: *UGH*. This assertion turned out to be profoundly dangerous.
#I'm currently unclear why, but the following example triggers this:
# from beartype.vale import IsInstance
# from beartype import beartype
# from typing import Annotated
# from collections.abc import Sequence
# SequenceNonstrOfStr = Annotated[Sequence[str], ~IsInstance[str]]
#
# @beartype
# def test(names: SequenceNonstrOfStr | str, / ) -> bool:
# return True
#Until we investigate this further, the only sane approach is to
#temporarily disable this. Let's pretend I know what I'm talking about.

# If this child pith expression duplicates the current pith assignment
# expression, raise an exception. Why? Because there is *NO* benefit to
# assigning that to another local variable via another assignment
Expand All @@ -515,22 +529,22 @@ def _enqueue_hint_child(pith_child_expr: str) -> str:
# SyntaxError: invalid syntax
#
# Specifically, if it is *NOT* the case that...
assert not (
# If this is a child hint rather than the root hint *AND*...
#
# The root hint is intentionally ignored, as that hint is already
# guaranteed to be an identifier rather than assignment expression.
hints_meta_index_curr and # pith_child_expr is not VAR_NAME_PITH_ROOT and
# This child pith expression duplicates the current pith assignment
# expression...
pith_child_expr is pith_curr_assign_expr
# Then raise an "AssertionError" exception.
), (
f'{EXCEPTION_PREFIX_HINT}{repr(hint_curr)} '
f'child pith expression {repr(pith_child_expr)} duplicates '
f'current pith assignment expression '
f'{repr(pith_curr_assign_expr)}.'
)
# assert not (
# # If this is a child hint rather than the root hint *AND*...
# #
# # The root hint is intentionally ignored, as that hint is already
# # guaranteed to be an identifier rather than assignment expression.
# hints_meta_index_curr and # pith_child_expr is not VAR_NAME_PITH_ROOT and
# # This child pith expression duplicates the current pith assignment
# # expression...
# pith_child_expr is pith_curr_assign_expr
# # Then raise an "AssertionError" exception.
# ), (
# f'{EXCEPTION_PREFIX_HINT}{repr(hint_curr)} '
# f'child pith expression {repr(pith_child_expr)} duplicates '
# f'current pith assignment expression '
# f'{repr(pith_curr_assign_expr)}.'
# )
# Else, the current child pith expression does *NOT* duplicate the
# current pith assignment expression.

Expand Down
11 changes: 11 additions & 0 deletions beartype/_decor/wrap/_wrapargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ def code_check_args(bear_call: BeartypeCall) -> str:
#obviously awful. Ideally, that tester *ABSOLUTELY* should
#resolve relative forward references. Until it does, however,
#this is verboten dark magic that is unsafe in the general case.
#FIXME: Note that there exist even *MORE* edge cases, however:
#@dataclass fields, which violate typing semantics: e.g.,
# from dataclasses import dataclass, field
# from typing import Dict
#
# from beartype import beartype
#
# @beartype
# @dataclass
# class A:
# test_dict: Dict[str, str] = field(default_factory=dict)
#FIXME: Once this has been repaired, please reenable:
#* The "test_decor_arg_kind_flex_optional" unit test.

Expand Down
2 changes: 1 addition & 1 deletion beartype/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _convert_version_str_to_tuple(version_str: str): # -> _Tuple[int, ...]:
# For further details, see http://semver.org.
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

VERSION = '0.18.1'
VERSION = '0.18.2'
'''
Human-readable package version as a ``.``-delimited string.
'''
Expand Down

0 comments on commit de3f94d

Please sign in to comment.