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

chore: Do not ignore jsonschema in mypy checks #1456

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ ignore_missing_imports = True
[mypy-pandas.*]
ignore_missing_imports = True

[mypy-jsonschema.*]
ignore_missing_imports = True

[mypy-jsonpath_ng.*]
ignore_missing_imports = True

Expand Down
3 changes: 2 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def mypy(session: Session) -> None:
"pytest",
"importlib-resources",
"sqlalchemy2-stubs",
"types-jsonschema",
"types-python-dateutil",
"types-requests",
"types-pytz",
"types-requests",
"types-simplejson",
"types-PyYAML",
)
Expand Down
72 changes: 17 additions & 55 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ testing = [
"pytest-durations"
]

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
# snowflake-connector-python = "2.0.4" # Removed: Too many version conflicts!
commitizen-version-bump = { git = "https://github.com/meltano/commitizen-version-bump.git", branch = "main" }
xdoctest = "^1.1.1"
Expand All @@ -110,12 +110,14 @@ freezegun = "^1.2.2"
viztracer = {version = "^0.15.6", optional = true, python = "<3.11"}
requests-mock = "^1.10.0"
sqlalchemy2-stubs = {version = "^0.0.2a32", allow-prereleases = true}
types-jsonschema = "^4.17.0.6"
types-python-dateutil = "^2.8.19"
types-pytz = "^2022.7.1.2"
types-requests = "^2.28.11"
types-simplejson = "^3.18.0"
types-PyYAML = "^6.0.12"
coverage = {extras = ["toml"], version = "^7.2"}
pytest-snapshot = "^0.9.0"

# Cookiecutter tests
black = "^23.1"
Expand All @@ -124,9 +126,6 @@ flake8 = "^3.9.0"
flake8-annotations = "^2.9.1"
flake8-docstrings = "^1.7.0"

[tool.poetry.group.dev.dependencies]
pytest-snapshot = "^0.9.0"

[tool.black]
exclude = ".*simpleeval.*"

Expand Down
2 changes: 1 addition & 1 deletion singer_sdk/_singerlib/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class _SchemaKey:

def resolve_schema_references(
schema: dict[str, t.Any],
refs: dict[str, dict] | None = None,
refs: dict[str, str] | None = None,
) -> dict:
"""Resolves and replaces json-schema $refs with the appropriate dict.

Expand Down
20 changes: 10 additions & 10 deletions singer_sdk/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Any, Callable, Mapping, cast

import click
from jsonschema import Draft7Validator, SchemaError, ValidationError
from jsonschema import Draft7Validator

from singer_sdk import metrics
from singer_sdk.configuration._dict_config import parse_environment_config
Expand Down Expand Up @@ -228,19 +228,18 @@ def _validate_config(
errors: list[str] = []
log_fn = self.logger.info
config_jsonschema = self.config_jsonschema

if config_jsonschema:
self.append_builtin_config(config_jsonschema)
try:
self.logger.debug(
f"Validating config using jsonschema: {config_jsonschema}"
)
validator = JSONSchemaValidator(config_jsonschema)
validator.validate(self._config)
except (ValidationError, SchemaError) as ex:
errors.append(str(ex.message))
self.logger.debug(
f"Validating config using jsonschema: {config_jsonschema}"
)
validator = JSONSchemaValidator(config_jsonschema)
errors = [e.message for e in validator.iter_errors(self._config)]
edgarrmondragon marked this conversation as resolved.
Show resolved Hide resolved

if errors:
summary = (
f"Config validation failed: {f'; '.join(errors)}\n"
f"Config validation failed: {'; '.join(errors)}\n"
f"JSONSchema was: {config_jsonschema}"
)
if raise_errors:
Expand All @@ -251,6 +250,7 @@ def _validate_config(
summary = f"Config validation passed with {len(warnings)} warnings."
for warning in warnings:
summary += f"\n{warning}"

if warnings_as_errors and raise_errors and warnings:
raise ConfigValidationError(
f"One or more warnings ocurred during validation: {warnings}"
Expand Down
33 changes: 27 additions & 6 deletions singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@

import json
import sys
from typing import Any, Generic, ItemsView, Mapping, TypeVar, Union, cast
from typing import (
Any,
Generator,
Generic,
ItemsView,
Mapping,
MutableMapping,
TypeVar,
Union,
cast,
)

import sqlalchemy
from jsonschema import validators
from jsonschema import ValidationError, Validator, validators

from singer_sdk.helpers._classproperty import classproperty
from singer_sdk.helpers._typing import (
Expand Down Expand Up @@ -107,18 +117,29 @@
]


def extend_validator_with_defaults(validator_class): # noqa
def extend_validator_with_defaults(validator_class): # noqa: ANN001, ANN201
"""Fill in defaults, before validating with the provided JSON Schema Validator.

See https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance # noqa
for details.

Args:
validator_class: The JSON Schema Validator class to extend.

Returns:
The extended JSON Schema Validator class.
"""
validate_properties = validator_class.VALIDATORS["properties"]

def set_defaults(validator, properties, instance, schema): # noqa
for property, subschema in properties.items():
def set_defaults(
validator: Validator,
properties: Mapping[str, dict],
instance: MutableMapping[str, Any],
schema: dict,
) -> Generator[ValidationError, None, None]:
for prop, subschema in properties.items():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
instance.setdefault(prop, subschema["default"])

yield from validate_properties(
validator,
Expand Down