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

Fix inheritance edge cases for type parameter resolution #230

Merged
merged 14 commits into from
Jan 15, 2025

Conversation

brentyi
Copy link
Owner

@brentyi brentyi commented Jan 15, 2025

Fixed some errors when resolving type parameters.

(1) Reusing the same TypeVar in multiple levels of an inheritance tree is allowed in Python, but was previously broken:

import dataclasses
from typing import Generic, TypeVar
import tyro

T = TypeVar("T")

@dataclasses.dataclass(frozen=True)
class A(Generic[T]):
    x: T

@dataclasses.dataclass(frozen=True)
class B(A[int], Generic[T]):
    y: T

# now works!
args = tyro.cli(B[str])

(2) We now correctly traverse inheritance trees when resolving type parameters that are used in __init__ and __new__ signatures. This fixes #229:

from pydantic import BaseModel
import tyro

class AConfig(BaseModel):
    a: int

class AModel[TContainsAConfig: AConfig]:
    def __init__(self, config: TContainsAConfig):
        self.config = config

class ABConfig(AConfig):
    b: int

class ABModel[TContainsABConfig: ABConfig](AModel[TContainsABConfig]):
    pass

class ABCConfig(ABConfig):
    c: int

class ABCModel(ABModel[ABCConfig]):
    pass

# now works!
tyro.cli(ABCModel)

Copy link

codecov bot commented Jan 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.82%. Comparing base (6761eed) to head (8265262).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #230   +/-   ##
=======================================
  Coverage   99.82%   99.82%           
=======================================
  Files          28       28           
  Lines        2308     2320   +12     
=======================================
+ Hits         2304     2316   +12     
  Misses          4        4           
Flag Coverage Δ
unittests 99.82% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@brentyi brentyi merged commit 49a5649 into main Jan 15, 2025
14 checks passed
@brentyi brentyi deleted the brent/generic_fixes_jan14 branch January 15, 2025 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tyro not following typevars through a second level of inheritance
1 participant