Skip to content
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

"Cannot determine where X is coming from" with TypedDict inheritance #504

Closed
slhck opened this issue Feb 3, 2023 · 2 comments
Closed

"Cannot determine where X is coming from" with TypedDict inheritance #504

slhck opened this issue Feb 3, 2023 · 2 comments
Labels

Comments

@slhck
Copy link

slhck commented Feb 3, 2023

Problem Description

I cannot get annotations to work with inherited TypedDics.

Steps to reproduce the behavior:

Consider the following TypedDicts in api.py

class GeneralApiParams(TypedDict):
    start_time: NotRequired[str]
    """The start time of the measurement in ISO-8601 format."""
    end_time: NotRequired[str]
    """The end time of the measurement in ISO-8601 format."""
    measurement_id: NotRequired[int]
    """The measurement ID."""
    client_uuid: NotRequired[str]
    """The client UUID."""
    tags: NotRequired[List[str]]
    """The tags of the client."""


class MeasurementsApiParams(GeneralApiParams):
    type: NotRequired[
        Literal[
            "VideoMeasurement",
            "WebMeasurement",
            "SpeedtestMeasurement",
            "NetworkMeasurement",
        ]
    ]
    """The type of the measurement."""

Which I import in __init__.py like so:

from .api import (
    GeneralApiParams,
    MeasurementsApiParams,
)

__all__ = [
    "GeneralApiParams",
    "MeasurementsApiParams",
]

Running pdoc gets me:

➜ pdoc -d google -o docs ./measurement_api
Warn: Cannot determine where measurement_api.MeasurementsApiParams.start_time is taken from, assuming current file. (.../lib/python3.11/site-packages/pdoc/doc.py:647)
Warn: Cannot determine where measurement_api.MeasurementsApiParams.end_time is taken from, assuming current file. (.../lib/python3.11/site-packages/pdoc/doc.py:647)
Warn: Cannot determine where measurement_api.MeasurementsApiParams.measurement_id is taken from, assuming current file. (.../lib/python3.11/site-packages/pdoc/doc.py:647)
Warn: Cannot determine where measurement_api.MeasurementsApiParams.client_uuid is taken from, assuming current file. (.../lib/python3.11/site-packages/pdoc/doc.py:647)
Warn: Cannot determine where measurement_api.MeasurementsApiParams.tags is taken from, assuming current file. (.../lib/python3.11/site-packages/pdoc/doc.py:647)

The resulting documentation shows:

image

So it shows as inherting from a builtin dict, which it should not, because it inherits from the other TypedDict.

System Information

pdoc: 12.3.1
Python: 3.11.0
Platform: macOS-12.6.3-arm64-arm-64bit
@slhck slhck added the bug label Feb 3, 2023
mhils added a commit to mhils/pdoc that referenced this issue Feb 3, 2023
mhils added a commit to mhils/pdoc that referenced this issue Feb 3, 2023
@mhils
Copy link
Member

mhils commented Feb 3, 2023

Thanks for the nice report. This is a relatively fundamental problem because TypedDict botches its __mro__, which means we normally cannot determine the base class via dynamic analysis. @krassowski came up with a nice workaround in sphinx-doc/sphinx#9290, which I've adapted for pdoc. If you add TypedDict as a second base class for MeasurementsApiParams, things should now work as expected once #505 is in. It would be nice if we wouldn't have to do this, but that requires an upstream fix in CPython. :)

@mhils mhils closed this as completed in f5fde4b Feb 4, 2023
@slhck
Copy link
Author

slhck commented Feb 4, 2023

Thanks for the quick response and fix! 🎉

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

No branches or pull requests

2 participants