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

Type schema attribute as Any #521

Closed cdce8p closed 2 months ago

cdce8p commented 2 months ago

Schemable is great as parameter type. However, without a dedicated type annotation mypy will also use it as argument type for Schema.schema and Marker.schema which just creates unhelpful errors when used

error: Unsupported right operand type for in ("Schema | Object | Mapping[Any, Any] |
    list[Any] | tuple[Any, ...] | <12 more items> | None")  [operator]
error: Item "Schema" of "Schema | Object | Mapping[Any, Any] | list[Any] | tuple[Any, ...] |
    <12 more items> | None" has no attribute "items"  [union-attr]

Typing it as Any will "disable" type checking when using the schema attribute, while still providing the type safety for the parameter type.

An alternative would have been #520. However that comes with its own set of issues as now all Schemas are generic and need additional type annotations.

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

alecthomas commented 2 months ago

Thanks!