Skip to content

Commit

Permalink
fix(typing): Resolve misc type ignores in schemapi.py (#3545)
Browse files Browse the repository at this point in the history
* chore(typing): Add `pyright` ignores in `schemapi.py`

* fix(typing): Address most issues in `_FromDict.from_dict`

This method is far too dynamic to fix everything.
Removed the need for one `mypy` specific ignore as well.

* chore(typing): Ignore caught AttributeError

* chore(typing): Add ignore in `validate_jsonschema`

* fix(typing): Resolve `pyright` & `mypy` errors in `validate_jsonschema`

* build: run `generate-schema-wrapper`
  • Loading branch information
dangotbanned authored Aug 18, 2024
1 parent c9f9438 commit c7e1e83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
36 changes: 17 additions & 19 deletions altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def validate_jsonschema(
rootschema: dict[str, Any] | None = ...,
*,
raise_error: Literal[True] = ...,
) -> None: ...
) -> Never: ...


@overload
Expand All @@ -129,11 +129,11 @@ def validate_jsonschema(

def validate_jsonschema(
spec,
schema,
rootschema=None,
schema: dict[str, Any],
rootschema: dict[str, Any] | None = None,
*,
raise_error=True,
):
raise_error: bool = True,
) -> jsonschema.exceptions.ValidationError | None:
"""
Validates the passed in spec against the schema in the context of the rootschema.
Expand All @@ -150,7 +150,7 @@ def validate_jsonschema(

# Nothing special about this first error but we need to choose one
# which can be raised
main_error = next(iter(grouped_errors.values()))[0]
main_error: Any = next(iter(grouped_errors.values()))[0]
# All errors are then attached as a new attribute to ValidationError so that
# they can be used in SchemaValidationError to craft a more helpful
# error message. Setting a new attribute like this is not ideal as
Expand Down Expand Up @@ -944,7 +944,7 @@ def __getattr__(self, attr):
return self._kwds[attr]
else:
try:
_getattr = super().__getattr__
_getattr = super().__getattr__ # pyright: ignore[reportAttributeAccessIssue]
except AttributeError:
_getattr = super().__getattribute__
return _getattr(attr)
Expand Down Expand Up @@ -1193,9 +1193,7 @@ def validate(
schema = cls._schema
# For the benefit of mypy
assert schema is not None
return validate_jsonschema(
instance, schema, rootschema=cls._rootschema or cls._schema
)
validate_jsonschema(instance, schema, rootschema=cls._rootschema or cls._schema)

@classmethod
def resolve_references(cls, schema: dict[str, Any] | None = None) -> dict[str, Any]:
Expand All @@ -1221,7 +1219,7 @@ def validate_property(
np_opt = sys.modules.get("numpy")
value = _todict(value, context={}, np_opt=np_opt, pd_opt=pd_opt)
props = cls.resolve_references(schema or cls._schema).get("properties", {})
return validate_jsonschema(
validate_jsonschema(
value, props.get(name, {}), rootschema=cls._rootschema or cls._schema
)

Expand Down Expand Up @@ -1323,11 +1321,11 @@ def from_dict(
@overload
def from_dict(
self,
dct: dict[str, Any],
tp: None = ...,
dct: dict[str, Any] | list[dict[str, Any]],
tp: Any = ...,
schema: Any = ...,
rootschema: None = ...,
default_class: type[TSchemaBase] = ...,
rootschema: Any = ...,
default_class: type[TSchemaBase] = ..., # pyright: ignore[reportInvalidTypeVarUse]
) -> TSchemaBase: ...
@overload
def from_dict(
Expand Down Expand Up @@ -1363,15 +1361,15 @@ def from_dict(
schema: dict[str, Any] | None = None,
rootschema: dict[str, Any] | None = None,
default_class: Any = _passthrough,
) -> TSchemaBase:
) -> TSchemaBase | SchemaBase:
"""Construct an object from a dict representation."""
target_tp: type[TSchemaBase]
target_tp: Any
current_schema: dict[str, Any]
if isinstance(dct, SchemaBase):
return dct # type: ignore[return-value]
return dct
elif tp is not None:
current_schema = tp._schema
root_schema = rootschema or tp._rootschema or current_schema
root_schema: dict[str, Any] = rootschema or tp._rootschema or current_schema
target_tp = tp
elif schema is not None:
# If there are multiple matches, we use the first one in the dict.
Expand Down
36 changes: 17 additions & 19 deletions tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def validate_jsonschema(
rootschema: dict[str, Any] | None = ...,
*,
raise_error: Literal[True] = ...,
) -> None: ...
) -> Never: ...


@overload
Expand All @@ -127,11 +127,11 @@ def validate_jsonschema(

def validate_jsonschema(
spec,
schema,
rootschema=None,
schema: dict[str, Any],
rootschema: dict[str, Any] | None = None,
*,
raise_error=True,
):
raise_error: bool = True,
) -> jsonschema.exceptions.ValidationError | None:
"""
Validates the passed in spec against the schema in the context of the rootschema.
Expand All @@ -148,7 +148,7 @@ def validate_jsonschema(

# Nothing special about this first error but we need to choose one
# which can be raised
main_error = next(iter(grouped_errors.values()))[0]
main_error: Any = next(iter(grouped_errors.values()))[0]
# All errors are then attached as a new attribute to ValidationError so that
# they can be used in SchemaValidationError to craft a more helpful
# error message. Setting a new attribute like this is not ideal as
Expand Down Expand Up @@ -942,7 +942,7 @@ def __getattr__(self, attr):
return self._kwds[attr]
else:
try:
_getattr = super().__getattr__
_getattr = super().__getattr__ # pyright: ignore[reportAttributeAccessIssue]
except AttributeError:
_getattr = super().__getattribute__
return _getattr(attr)
Expand Down Expand Up @@ -1191,9 +1191,7 @@ def validate(
schema = cls._schema
# For the benefit of mypy
assert schema is not None
return validate_jsonschema(
instance, schema, rootschema=cls._rootschema or cls._schema
)
validate_jsonschema(instance, schema, rootschema=cls._rootschema or cls._schema)

@classmethod
def resolve_references(cls, schema: dict[str, Any] | None = None) -> dict[str, Any]:
Expand All @@ -1219,7 +1217,7 @@ def validate_property(
np_opt = sys.modules.get("numpy")
value = _todict(value, context={}, np_opt=np_opt, pd_opt=pd_opt)
props = cls.resolve_references(schema or cls._schema).get("properties", {})
return validate_jsonschema(
validate_jsonschema(
value, props.get(name, {}), rootschema=cls._rootschema or cls._schema
)

Expand Down Expand Up @@ -1321,11 +1319,11 @@ def from_dict(
@overload
def from_dict(
self,
dct: dict[str, Any],
tp: None = ...,
dct: dict[str, Any] | list[dict[str, Any]],
tp: Any = ...,
schema: Any = ...,
rootschema: None = ...,
default_class: type[TSchemaBase] = ...,
rootschema: Any = ...,
default_class: type[TSchemaBase] = ..., # pyright: ignore[reportInvalidTypeVarUse]
) -> TSchemaBase: ...
@overload
def from_dict(
Expand Down Expand Up @@ -1361,15 +1359,15 @@ def from_dict(
schema: dict[str, Any] | None = None,
rootschema: dict[str, Any] | None = None,
default_class: Any = _passthrough,
) -> TSchemaBase:
) -> TSchemaBase | SchemaBase:
"""Construct an object from a dict representation."""
target_tp: type[TSchemaBase]
target_tp: Any
current_schema: dict[str, Any]
if isinstance(dct, SchemaBase):
return dct # type: ignore[return-value]
return dct
elif tp is not None:
current_schema = tp._schema
root_schema = rootschema or tp._rootschema or current_schema
root_schema: dict[str, Any] = rootschema or tp._rootschema or current_schema
target_tp = tp
elif schema is not None:
# If there are multiple matches, we use the first one in the dict.
Expand Down

0 comments on commit c7e1e83

Please sign in to comment.