brentyi / tyro

CLI interfaces & config objects, from types
https://brentyi.github.io/tyro
MIT License
467 stars 23 forks source link

narrow_collection_types crashes when type is Type and class defines __len__ #136

Closed atonderski closed 5 months ago

atonderski commented 5 months ago

Hi,

I think I ran into a bug with the following config:

@dataclass
class Config:
    _target: Type = field(default_factory=lambda: MyClass)

class MyClass:
    def __len__(self):
        return 0

tyro.cli(Config)

this triggers TypeError: object of type 'type' has no len() inside

    typ: TypeOrCallable, default_instance: Any
) -> TypeOrCallable:
    """TypeForm narrowing for containers. Infers types of container contents."""
    if hasattr(default_instance, "__len__") and len(default_instance) == 0:
        return typ

at tyro/_resolver.py:240

brentyi commented 5 months ago

Hi @atonderski, thanks for filing this issue + instructions for reproducing!

I just pushed a release with this fixed. Also added your example to the tests to prevent regressions.

atonderski commented 5 months ago

Thanks for the quick fix!