Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

check_type does not check required fields when mixing required and non-required fields in TypedDict #101

Closed
ksaaskil opened this issue Dec 19, 2019 · 7 comments

Comments

@ksaaskil
Copy link

Hi, thanks for the awesome library! A recent fix for an issue (#94) added support for totality in TypedDict, thanks for that! I'm trying to mix required and non-required fields in TypedDict as instructed in mypy documentation:

class MovieBase(TypedDict):
    name: str
    year: int

class Movie(MovieBase, total=False):
    based_on: str

This definition should make name and year required fields in Movie.

However, check_type does not seem to complain if those are missing:

from typeguard import check_type

movie = Movie()   # Missing `name` and `year` , type-checker complains
check_type("movie", movie, Movie)  # Does not raise error but it should?

Any idea what might be wrong here? Thanks!

@agronholm
Copy link
Owner

How am I going to extract the information about MovieBase at run time? It doesn't show up in the MRO or the list of base classes of Movie.

@ksaaskil
Copy link
Author

I have no idea how that should be implemented 🙂 If it cannot be, maybe we could add a note to the documentation?

@agronholm
Copy link
Owner

Sounds reasonable.

@antonagestam
Copy link
Contributor

Note that this situation is addressed in Python 3.9 with dunder attributes for __required_keys__ and __optional_keys__. Would you consider reopening this? :)

@agronholm
Copy link
Owner

Sure.

@agronholm agronholm reopened this Oct 16, 2020
@agronholm agronholm removed the wontfix label Oct 16, 2020
@agronholm
Copy link
Owner

It seems the new patch was botched:

>>> DummyDict = TypedDict('DummyDict', {'x': int, 'y': str}, total=False)
>>> DummyDict.__required_keys__
frozenset({'x', 'y'})

It's ignoring the total argument.

@agronholm
Copy link
Owner

I've submitted a fix against CPython: python/cpython#22736

If I add support for __required_keys__, then it will behave wrong with the 3.5 compatible syntax in Python 3.9.0. I'm not sure what to do.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants