-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
beartype_this_package()
exceptions improved.
This commit significantly improves exceptions raised by the `beartype.claw.beartype_this_package()` import hook for common use cases, resolving both issue #320 kindly submitted by Copenhagen superstar @komodovaran (Johannes Thomsen) and forum thread #330 kindly submitted by long-time `typing` fiend @JWCS (Jude). Specifically, `beartype_this_package()` now raises exception messages resembling the following when called directly from: * Top-level scripts: ```python beartype.roar.BeartypeClawHookUnpackagedException: Top-level script "/home/leycec/tmp/src/script.py" resides outside package structure. Consider calling another "beartype.claw" import hook. However, note that only other modules will be type-checked. "/home/leycec/tmp/src/script.py" itself will remain unchecked. All business logic should reside in submodules subsequently imported by "/home/leycec/tmp/src/script.py": e.g., # Instead of this at the top of "/home/leycec/tmp/src/script.py"... from beartype.claw import beartype_this_package # <-- you are here beartype_this_package() # <-- feels bad # ...pass the basename of the "src/" subdirectory explicitly. from beartype.claw import beartype_package # <-- you want to be here beartype_package("src") # <-- feels good from src.main_submodule import main_func # <-- still feels good main_func() # <-- *GOOD*! "beartype.claw" type-checks this some_global: str = 0xFEEDFACE # <-- *BAD*! "beartype.claw" ignores this This has been a message from your friendly neighbourhood bear. ``` * Top-level modules: ```python Top-level module "main.py" resides outside package structure but was *NOT* directly run as a script. "beartype.claw" import hooks require that modules either reside inside a package structure or be directly run as scripts. Since neither applies here, you are now off the deep end. @beartype no longer has any idea what is going on, sadly. Consider directly decorating classes and functions by the @beartype.beartype decorator instead: e.g., # Instead of this at the top of "main"... from beartype.claw import beartype_this_package # <-- you are here beartype_this_package() # <-- feels bad \n' # ...go old-school like it's 2017 and you just don't care. from beartype import beartype # <-- you want to be here @beartype # <-- feels good, yet kinda icky at same time def spicy_func() -> str: ... # <-- *GOOD*! @beartype type-checks this some_global: str = 0xFEEDFACE # <-- *BAD*! @beartype ignores this, but what can you do For your safety, @beartype will now crash and burn. ``` Unrelatedly, this commit also revises our ReadTheDocs (RTD)-hosted FAQ entry on `pytest-beartype` to document configuration via standard `pyproject.toml` files, resolving issue #327 kindly submitted by spaghetti-loving Bay Area pasta guru @jamesbraza (James Braza). (*Watery warts on a tart rotisserie!*)
- Loading branch information
Showing
13 changed files
with
349 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
Project-wide **code object globals** (i.e., global constants describing code | ||
objects of callables, classes, and modules). | ||
This private submodule is *not* intended for importation by downstream callers. | ||
''' | ||
|
||
# ....................{ STRINGS }.................... | ||
FUNC_CODEOBJ_NAME_MODULE = '<module>' | ||
''' | ||
Arbitrary string constant unconditionally assigned to the ``co_name`` instance | ||
variables of the code objects of all pure-Python modules (i.e., the top-most | ||
lexical scope of each module in the current call stack). | ||
This constant enables callers to reliably differentiate between code objects | ||
encapsulating: | ||
* Module scopes, whose ``co_name`` variable is this constant. | ||
* Callable scopes, whose ``co_name`` variable is *not* this constant. | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.