Comments (12)
Sorry, this is a documentation issue. bound_typevars
is kind of an output parameter. If provided, it grants _issubclass
the freedom to assign typevars as they occur. Only predefined mappings are checked, i.e. if the dict already contained an entry for C
. As a consequence, if a typevar occurs within a type in inconsistent manner, this will error. You can observe how it populates typevar bindings:
bound_typevars={}
_issubclass(L[float], C, bound_typevars=bound_typevars) # True
print(bound_typevars) # {~T: __main__.L[float]}
from pytypes.
Just looked into the doc and found out that bound_typevars
is not yet documented at all. So that's a todo then...
Maybe a flag to tell _issubclass
whether it is allowed to bind typevars or not would be good.
The usecase for this dict was to have kind of a memory between subsequent calls, i.e. if you want to check multiple types that share one or several typevars. On first occurrence they are assigned, but later an inconsistent assignment would cause an error.
from pytypes.
Yes, I understand this idea about the output/memory. This is necessary to type check outputs of the function for example.
But I do not understand why having this output changes the outcome. :-) I would assume that type-wise the output should be the same, no? Only that this memory is discarded?
from pytypes.
I didn't want unassigned typevars to be treated as Any
in general (e.g. if someone overlooks that some unspecified typevar is in the game they should get an alert).
Providing a dict for assignements is the way to tell _issubclass
that it's okay to assign them.
Maybe this behaviour should be reconsidered. At least I do see a usecase for a strict check i.e. only considering existing assignments. Maybe we could change the default value to {}
rather than None
.
from pytypes.
As of 74ef4b9 _issubclass
has the boolean-argument bound_typevars_readonly
(default: False
). If set to True
, _issubclass
will not assign typevars, but only check against existing ones. For strict test-cases...
Todo: Write test for this flag.
from pytypes.
Hm, nice. But I am not sure how does this help my case above? :-)
>>> type_util._issubclass(L[float], C, bound_typevars={})
True
>>> type_util._issubclass(L[float], C, bound_typevars_readonly=True)
False
>>> type_util._issubclass(L[float], C, bound_typevars_readonly=True, bound_typevars={})
False
I think this is related, but not sure if it addresses the question that I think type_util._issubclass(L[float], C)
should return True
by default.
from pytypes.
Only in the sense that bound_typevars_readonly=True
yields the behaviour you originally expected for bound_typevars={}
. I'm considering to use bound_typevars={}
as default rather than bound_typevars=None
.
from pytypes.
bound_typevars_readonly=True, bound_typevars={}
as a default? This sounds reasonable.
from pytypes.
Hmm, I thought bound_typevars_readonly=False, bound_typevars={}
. With bound_typevars_readonly=True
it would be the same behavior like currently with bound_typevars=None
, wouldn't?
from pytypes.
Oh, you are right.
from pytypes.
I think this can be closed now, no?
from pytypes.
I kept it open as a reminder to improve documentation. I think doc of _issubclass explains it properly now. I suppose the quick manual should mention this behavior as well. Anyway, I'm fine with closing now.
from pytypes.
Related Issues (20)
- Utility to validate stubfile signatures against .py implementations HOT 4
- pytypes is_of_type: counter-intuitive result on semi-primitive types like list and dict HOT 5
- is_of_type does not work as expected with Annotated[Generic[T], ...] HOT 1
- Some differences between Python 3.6 and 3.8 HOT 3
- New release HOT 1
- please improve error messages when functions decorated with @typechecked are called with incorrect types
- pytypes breaks @autoreload in a somewhat dangerous way HOT 8
- `is_subtype` fails with `List[TypeVar]`
- Infer return type of generic functions based on input
- Support Python 3.10 HOT 7
- @typechecked fails to correctly parse functions with PEP 3102
- Incorrect collections imports in type_util.py HOT 1
- VSCode incorrect decorated func hint HOT 7
- AttributeError: 'NoneType' object has no attribute '__bound__' HOT 5
- Partially-annotated functions with default parameter values result in errors. HOT 12
- Error in @typechecked with List[Dict[str, str]] HOT 1
- Remove backports.typing usage
- _check_caller_type can be broken by removing a blank line in typechecker_testhelper_py3.py
- typechecked(string) should have more consistent return type HOT 1
- Python 3.8 Compatibility HOT 1
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 pytypes.