-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__total__ is assigned True for types with optional fields #113
Comments
I see why. This is because of the way I build TypedDicts with mixed totality. As you see, this TypedDict has both optional and required fields: https://github.com/vemel/boto3_stubs_docs/blob/e4d1c97abceff6fc3e1bee0428d5f56d5581eee9/mypy_boto3_ec2/type_defs.md#spotfleetrequestconfigdatatypedef . So, totality for You can check if TypedDict has optional keys instead:
Let me if this works for you |
Thanks so much for the really fast response! Unfortunately typeguard is a downstream dependency for me so it wouldn't be easy. However, I did notice from mypy documentation that it's possible to override from mypy_boto3_ec2 import type_defs
from typing import TypedDict
print('RunInstancesRequestRequestTypeDef', type_defs.RunInstancesRequestRequestTypeDef.__total__)
class X(type_defs.RunInstancesRequestRequestTypeDef, total=False):
pass
print('X', X.__total__)
class Base(TypedDict):
pass
class Y(Base, type_defs.RunInstancesRequestRequestTypeDef):
pass
print('Y', Y.__total__)
class Z(Base, type_defs.RunInstancesRequestRequestTypeDef, total=False):
pass
print('Z', Z.__total__) Results:
Could that be a way forward? (It doesn't work as a quick work-around on my side unfortunately because of fields referencing other TypedDict types) |
Note that typeguard does actually check for Never-the-less, my feeling is that the transitivity of |
(It would also be difficult to require python 3.9 for this particular project I'm afraid - I'm just a contributor to it.) |
You can use |
Oh, I see! Would it make sense to change the headers so that I'm not sure how else to swap out >>> from typing_extensions import TypedDict
class X(TypedDict): pass
>>> hasattr(X, '__required_keys__')
True
>>> import typing
>>> class Y(typing.TypedDict): pass
>>> hasattr(Y, '__required_keys__')
False
>>> import mypy_boto3_ec2.type_defs as type_defs
>>> hasattr(type_defs.SpotFleetRequestConfigDataTypeDef, '__required_keys__')
False Thanks again! |
I realized that ideally I really would want to have available both total and non-total versions of the dictionaries for my use case, but then the only elegant solution seems to be python/mypy#4128 which mypy can't support right now. (So perhaps this issue ought to be closed for now.) |
Describe the bug
It appears that
__total__
is assignedTrue
in cases where the TypedDict should not be total. This ends up confusing tools like typeguard which uses it for totality checking(E.g. see https://github.com/agronholm/typeguard/blob/3f12d1fadb18aa34de6334010e833825b506164d/src/typeguard/__init__.py#L332-L353)
To Reproduce
Additional context
The text was updated successfully, but these errors were encountered: