agronholm / typeguard

Run-time type checker for Python
Other
1.56k stars 114 forks source link

There should be an option to process TYPE_CHECKING #484

Open jolaf opened 2 months ago

jolaf commented 2 months ago

Things to check first

Feature description

It would be nice for typeguard to actually process what's inside the if TYPE_CHECKING blocks, actually importing the referenced modules etc.

It would be nice to have an option to turn on this behavior.

Use case

That's not how an app would work without typeguard, but that would allow to check the app for type correctness more thoroughly.

agronholm commented 2 months ago

Would you consider becoming a maintainer for Typeguard? I don't have the bandwidth to keep up the development, as I'm mired in developing many other projects.

agronholm commented 2 months ago

This, for example, is a major effort for a run-time type checker which does not normally even see the if TYPE_CHECKING: blocks.

jolaf commented 2 months ago

Would you consider becoming a maintainer for Typeguard?

I would happily, but unfortunately I'm not up to the task. :(

jolaf commented 2 months ago

run-time type checker which does not normally even see the if TYPE_CHECKING: blocks

You mean they're somehow excluded from the source tree when importing the module?

Is it possible to just set TYPE_CHECKING to True when importing the module?

I was thinking that you're making a special effort to look into TYPE_CHECKING blocks to make sure the annotations mentioning things imported there are not producing errors. If TYPE_CHECKING is somehow set to True, those efforts may be just turned off.

agronholm commented 2 months ago

You mean they're somehow excluded from the source tree when importing the module?

A run-time type checker just sees what's there at run time, it doesn't normally inspect the source code, and this isn't even always possible (like in the REPL). Normally, if TYPE_CHECKING is set to False, which it is always at run time, no code in the block is executed so whatever imports you have there aren't done.

Typeguard goes out of its way to parse and modify the source code, but that comes with an additional, massive increase to complexity and a performance hit that some users are unhappy with.

Is it possible to just set TYPE_CHECKING to True when importing the module?

That's not a viable option. In these blocks, it's customary to do things that don't work at all at run time, like importing Typeshed types.

jolaf commented 2 months ago

That's not a viable option.

Well, everything I talk about here is only an option. It seems to me it would be nice if a user could choose, to execute TYPE_CHECKING blocks or not, in their particular case.