How to properly use beartype for a single internal module of a package?
#434
Replies: 6 comments 3 replies
-
|
Excellent questions! Happily, I can actually solve everything for once. Let's do this.
Right. Sadly, that's exactly what one would expect. I know. Kinda lame, isn't it? Allow me to now pontificate. Like all Python import hooks, The usual solution is thus to call tl;dr: Five-Second Solutions Feels GoodPlease call
Whichever you prefer! Any should work. Specifically, this is what I advise: # In any of the above "__init__" submodules:
from beartype.claw import beartype_package
beartype_package('A.B.C.D._private.lib')Oh – and note that
Sadness! I actually didn't know that. Ideally, @beartype should play nicely with everybody. @beartype strictly adheres to almost all PEP (Python Enhancement Proposal) standards. If @beartype fails to play nicely with If you find the time to produce a minimal-reproducible example (MRE) and a traceback, would you mind submitting a new issue to the @beartype issue tracker about this integration issue between @beartype + |
Beta Was this translation helpful? Give feedback.
-
|
Hey @leycec, thank you for the thorough response! I will try out the solution you mentioned about using As far as the @APP.command()
def sync(
targets: list[int] = typer.Argument(
...,
metavar="PID [PID [...]]",
help="Target PIDs to sync tile data for.",
),
submit_change: bool = typer.Option(
False,
rich_help_panel="Perforce Options",
help="Whether or not to submit the changelist with the downloaded tile data (if any).",
),
) -> None:
...So, for example, the |
Beta Was this translation helpful? Give feedback.
-
|
@leycec I'm probably still doing something wrong, but when I tried adding this """Private internal package."""
# ==Site-Packages==
from beartype.claw import beartype_package
####################################################################################################
# Main
####################################################################################################
beartype_package(f"{__package__}.lib")I also did check with a breakpoint right before the Is there a way to verify what hooks were registered and what functions/modules are affected? |
Beta Was this translation helpful? Give feedback.
-
|
@leycec Wow, that is quite the response! Thank you! Here is some more background on my use case:
I will try out what you suggested in the Really appreciate your thorough responses! |
Beta Was this translation helpful? Give feedback.
-
|
WOAH. You... you're doing everything right! I'm astonished, actually. Namespace packages are kinda esoteric. Your use case is ideal and perfect for namespace packages. My assumption was waaaay off. I humbly apologize for insinuating that your package structure was inappropriate or somehow "bad." Indeed, I'm bad. The design pattern you've adhered to is a flawless example of namespace packaging. Which leads us to only one valid conclusion, really:
Namespace packaging is really non-trivial. Usually, that's not an issue. In the case of import hooks, though, that's an issue. All import hooks (including those published through the Namespace packages are fundamentally different from standard packages. Thus, so is their importation. Thus, so is their implementation via the Let's open yet another feature request #438 for this! Someday, @beartype will actually work for you, @taranlu-houzz. Until then, I beg forbearance. ...get it? forbearance? i'll stop now. Forgive @beartype its sins. It knows not what it does. 🥹 |
Beta Was this translation helpful? Give feedback.
-
|
@leycec Haha, thanks for the info. Interesting to hear that namespace packages are such an issue. That is unfortunate. As a followup, I did try using Interestingly, @typing.no_type_check
@APP.command() # typer decorator
def my_func():
...I think that for now, I will probably just directly decorate the functions with I did run into one more interesting issue though. When I ran my tool via its cli command (as opposed to using the lib part), It would be nice to have official support for pretty Edit: Here is a link to the Edit: I tried using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My project has the following structure:
When I try to use
beartype.claw.beartype_this_package()orbeartype.claw.beartype_package(__name__)inlib, it doesn't seem to work. However, I am able to use thebeartype.beartypedecorator directly. The reason I am not usingbeartypein the package__init__is because the actual project usestyper, which does not play nicely withbeartype, so I just want to use strict typing for the internal library functions for now.Beta Was this translation helpful? Give feedback.
All reactions