alecthomas / voluptuous

CONTRIBUTIONS ONLY: Voluptuous, despite the name, is a Python data validation library.
https://pypi.org/project/voluptuous
BSD 3-Clause "New" or "Revised" License
1.81k stars 219 forks source link

Use TypeVar for Schema.schema type #520

Closed cdce8p closed 2 months ago

cdce8p commented 2 months ago

Schemable is great as a catch all case. However, the Union prevents further uses especially of the schema attribute. E.g.

schema = vol.Schema({"key": str})
schema.schema.items()  # should access the dict items used to create the schema

This PR changes the schema attribute type to a TypeVar. This allows for either implicit or explicit specialization by the user. It would fallback to typing.Any if type checkers can't infer the specialized type.

That's still better than the current situation as that just always emits an error.

error: Item "type" of "Schema | Object | Mapping[Any, Any] | list[Any] | tuple[Any, ...] |
    <12 more items> | None" has no attribute "__iter__" (not iterable)  [union-attr]

Refs https://github.com/home-assistant/core/pull/120268

cdce8p commented 2 months ago

Closed in favor of #521