pschanely / CrossHair

An analysis tool for Python that blurs the line between testing and type systems.
Other
1.04k stars 49 forks source link

Extending native types doesn't work as expected #184

Open pschanely opened 2 years ago

pschanely commented 2 years ago

As shown in this example, extending native types (that CrossHair cares about) doesn't work as expected.

I suspect there may be multiple layers of problems, the first of which is that a correct constructor signature is harder to deduce for these types.

cc @mristin

mristin commented 2 years ago

@pschanely thanks! Be aware that we used __new__ intensively to impose constraints on primitive types in https://github.com/mristin/python-by-contract. That was a "canonical" way for me, and also the efficient one as values can not be mutated.

pschanely commented 2 years ago

Indeed; I recall some of that! In general, CrossHair should be able to construct via the signatures on __new__ and __init__ appropriately.

Even after construction, though, I expect some additional hurdles for classes extending some of the core types: str, list, dict, etc. Needs more investigation!

I believe many of the python-by-contract classes were never actually instantiated - that is, the __new__ just returned its argument. Those cases hopefully aren't subject to such challenges.