Comments (4)
First, thank you for the very detailed repro instructions!
This is a tricky problem and solving it would unlock a lot of use cases for CrossHair. Unfortunately, it is also one that I'm going to need to think about. First, I'll start with some internals:
Unlike mypy, CrossHair works by actually importing and running your code. It just passes very special fake values to your functions to do its magic. It needs to understand both the function under test, as well as the types on the supplied parameters. In your example, for instance, if we remove the TYPE_CHECKING
guard, simply running python foobar/bar.py
(without CrossHair) will produce the same error.
That said, perhaps we can do some trickery. I'm imagining something like: import with TYPE_CHECKING
off and then re-import with it on. I'll experiment with this over the next few days and report back with what I discover.
from crosshair.
I understand. I do consider circular dependencies as unwanted design, to be avoided as much as possible. Unfortunately I'm working with a code base that does have 'm, in large amounts, and no time to remove/replace them.
What I can do and might help find a way around it, is the way I do these imports:
I'm also not very fond of using the TYPE_CHECKING flag; very framework specific, limits my control over the code. So I'd rather do without it. Maybe the trickery can cover something like replacing the import statement with a function that does conditional imports:
from no.circular.dependency.module.a_class import A_class
cond_import({"module_name":"class(es)"})
class ThisClass:
...
Then in the cond_import function, I have a single TYPE_CHECKING flag and all I need to do the importing and maybe break/prevent circular importing and what not?
But maybe this b0rks MyPy...
from crosshair.
Yeah, I suspect that hiding the TYPE_CHECKING guard will confuse type checkers.
However, I just attempted an odd sort of fix where CrossHair will detect the presence of these guards and attempt a re-import with them turned on. Not all modules are guaranteed to re-import properly, but this behavior still seems like a good compromise to me.
@CoenSchalkwijk Would you try again at HEAD and let me know how it goes? Thanks for your help here!
from crosshair.
Discovered in #172, I am newly of the opinion that it's a mistake to even attempt an import with TYPE_CHECKING=True
.
pytorch
, for instance, uses the guard to import via .pyi
files, which are obviously not importable at runtime.
Sadly, this means that crosshair will be able to understand fewer type annotations. (including the one in the original example) Its behavior will correspond directly to the type annotations that are present at runtime, however: so that behavior is at least easy to explain. A different way to say this is that CrossHair's ability to understand type annotations will be roughly similar to any other tool that analyzes type annotations at runtime.
I am nevertheless saddened that we won't be able to handle this kind of situation. I have some optimism that Python is (slowly) evolving in directions that enhances typing use cases at runtime, so perhaps this wart becomes less apparent over time.
from crosshair.
Related Issues (20)
- Support atomic grouping and possessive quantifiers
- Support correct and compilable representation of enum values in the cover/pytest mode HOT 7
- Support pytest.raises Assertion / Exception match parameter when there is a message in exception HOT 1
- Produce the correct import statements for pytest output on cover command HOT 1
- CrossHair is missing symbolic support for `str.split` without arguments
- Support Python 3.12 HOT 1
- Implement true floating point semantics
- Unexpected checking results for functions with/without type annotations HOT 4
- Generic objects, when realized to strings, raise TypeError on concatenation.
- CrosshairInternal error raised when attempting to use symbolic integer as a function HOT 1
- Ensure all isinstance checks account for undecided types
- cover command error: (AttributeError("'AlgebraicNumRef' object has no attribute 'as_fraction'") HOT 2
- Unexpectedly long run times on a short example containing floats HOT 2
- Improve premature realization heuristics
- Heuristics for lengths of sequence-like symbolics
- Update Z3 version HOT 1
- Implement pluggable smtlib2 backends
- Can I use a seed to start exploration when using cover command? HOT 1
- Confusing findings for dictionary constructors HOT 5
- Add support for NewType HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crosshair.