3 ms·
They made a flag TYPE_CHECKING to avoid circular import in type checking. Its ugly but works. https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKIN
by h8hawk 7y ago
They made a flag TYPE_CHECKING to avoid circular import in type checking. Its ugly but works.
https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING https://docs.python.org/3/library/typing.html#typing.TYPE_CH...
- weberc2 7y agoAlso the circular imports was the least of the problems I’ve run into with mypy. They still can’t model a JSON type because it is recursive.
- earthtolazlo 7y agoRecursive types were added in Typescript 3.7. Hopefully Python takes a page from their book.
- takeda 7y agoCan you show example? I believe you can use TypedDict to express that.
- weberc2 7y agoIt’s roughly JSON = Union[Dict[str, JSON], List[JSON], str, bool, int, NoneType] TypedDict isn’t for arbitrary JSON, but it is really cool.
- takeda 7y agoAh I see, TypedDict won't be useful here. Looks like there is a work in progress though[1] and people in the ticket provided some workarounds[2][3] for now. [1] https://github.com/python/mypy/issues/731 https://github.com/python/mypy/issues/731 [2] https://github.com/python/mypy/issues/731#issuecomment-539905783 https://github.com/python/mypy/issues/731#issuecomment-53990... [3] https://gist.github.com/catb0t/bd82f7815b7e95b5dd3c3ad294f3cbbf https://gist.github.com/catb0t/bd82f7815b7e95b5dd3c3ad294f3c...