Skip to content

Commit

Permalink
enable strict type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Jan 31, 2023
1 parent c9aae93 commit 9318b72
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 61 deletions.
21 changes: 2 additions & 19 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
[mypy]
strict = True
show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true
warn_unreachable = True

# --strict options as of mypy 0.910
warn_unused_configs = True
disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True
no_implicit_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_return_any = True
implicit_reexport = False
strict_equality = True
enable_recursive_aliases = True

[mypy-ruamel.*]
ignore_errors = True
30 changes: 13 additions & 17 deletions schema_salad/avro/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,11 @@ def __init__(
) -> None:
# Ensure valid ctor args
if not name:
fail_msg = "Named Schemas must have a non-empty name."
raise SchemaParseException(fail_msg)
raise SchemaParseException("Named Schemas must have a non-empty name.")
elif not isinstance(name, str):
fail_msg = "The name property must be a string."
raise SchemaParseException(fail_msg)
raise SchemaParseException("The name property must be a string.")
elif namespace is not None and not isinstance(namespace, str):
fail_msg = "The namespace property must be a string."
raise SchemaParseException(fail_msg)
raise SchemaParseException("The namespace property must be a string.")
if names is None:
raise SchemaParseException("Must provide Names.")

Expand Down Expand Up @@ -289,7 +286,7 @@ def __init__(
fail_msg = "Fields must have a non-empty name."
raise SchemaParseException(fail_msg)
elif not isinstance(name, str):
fail_msg = "The name property must be a string."
fail_msg = "The name property must be a string." # type: ignore[unreachable]
raise SchemaParseException(fail_msg)
elif order is not None and order not in VALID_FIELD_SORT_ORDERS:
fail_msg = f"The order property {order} is not valid."
Expand Down Expand Up @@ -368,14 +365,13 @@ def __init__(
) -> None:
# Ensure valid ctor args
if not isinstance(symbols, list):
fail_msg = "Enum Schema requires a JSON array for the symbols property."
raise AvroException(fail_msg)
raise AvroException(
"Enum Schema requires a JSON array for the symbols property."
)
elif False in [isinstance(s, str) for s in symbols]:
fail_msg = "Enum Schema requires all symbols to be JSON strings."
raise AvroException(fail_msg)
raise AvroException("Enum Schema requires all symbols to be JSON strings.")
elif len(set(symbols)) < len(symbols):
fail_msg = f"Duplicate symbol: {symbols}"
raise AvroException(fail_msg)
raise AvroException(f"Duplicate symbol: {symbols}")

# Call parent ctor
NamedSchema.__init__(self, "enum", name, namespace, names, other_props)
Expand Down Expand Up @@ -440,8 +436,7 @@ def __init__(
if names is None:
raise SchemaParseException("Must provide Names.")
if not isinstance(schemas, list):
fail_msg = "Union schema requires a list of schemas."
raise SchemaParseException(fail_msg)
raise SchemaParseException("Union schema requires a list of schemas.")

# Call parent ctor
Schema.__init__(self, "union")
Expand Down Expand Up @@ -526,8 +521,9 @@ def __init__(
) -> None:
# Ensure valid ctor args
if not isinstance(fields, list):
fail_msg = "Fields property must be a list of Avro schemas."
raise SchemaParseException(fail_msg)
raise SchemaParseException(
"Fields property must be a list of Avro schemas."
)

# Call parent ctor (adds own name to namespace, too)
NamedSchema.__init__(self, schema_type, name, namespace, names, other_props)
Expand Down
2 changes: 1 addition & 1 deletion schema_salad/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def main(argsl: Optional[List[str]] = None) -> int:
json_dump(avsc_obj, fp=sys.stdout, indent=4, default=str)
return 1
else:
_logger.error("Schema `%s` must be a list.", args.schema)
_logger.error("Schema `%s` must be a list.", args.schema) # type: ignore[unreachable]
return 1

# Optionally print Avro-compatible schema from schema
Expand Down
5 changes: 1 addition & 4 deletions schema_salad/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,7 @@ def _document_load_by_url(
doc_url, frg = urldefrag(url)

text = loadingOptions.fetcher.fetch_text(doc_url)
if isinstance(text, bytes):
textIO = StringIO(text.decode("utf-8"))
else:
textIO = StringIO(text)
textIO = StringIO(text)
textIO.name = str(doc_url)
yaml = yaml_no_ts()
result = yaml.load(textIO)
Expand Down
5 changes: 1 addition & 4 deletions schema_salad/python_codegen_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,7 @@ def _document_load_by_url(
doc_url, frg = urldefrag(url)

text = loadingOptions.fetcher.fetch_text(doc_url)
if isinstance(text, bytes):
textIO = StringIO(text.decode("utf-8"))
else:
textIO = StringIO(text)
textIO = StringIO(text)
textIO.name = str(doc_url)
yaml = yaml_no_ts()
result = yaml.load(textIO)
Expand Down
20 changes: 6 additions & 14 deletions schema_salad/ref_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,9 @@ def resolve_all(
)
elif isinstance(document, CommentedSeq):
pass
elif isinstance(document, (list, dict)):
elif isinstance(document, (list, dict)): # type: ignore[unreachable]
raise ValidationException(
"Expected CommentedMap or CommentedSeq, got {}: `{}`".format(
type(document), document
)
f"Expected CommentedMap or CommentedSeq, got {type(document)}: {document!r}"
)
else:
return (document, metadata)
Expand Down Expand Up @@ -1008,11 +1006,7 @@ def fetch(
return self.idx[url]
try:
text = self.fetch_text(url, content_types=content_types)
if isinstance(text, bytes):
textIO = StringIO(text.decode("utf-8"))

else:
textIO = StringIO(text)
textIO = StringIO(text)
textIO.name = str(url)
yaml = yaml_no_ts()
attachments = yaml.load_all(textIO)
Expand Down Expand Up @@ -1070,10 +1064,10 @@ def validate_scoped(self, field: str, link: str, docid: str) -> str:
def validate_link(
self,
field: str,
link: Union[str, CommentedSeq, CommentedMap],
link: Union[str, CommentedSeq, CommentedMap, None],
docid: str,
all_doc_ids: Dict[str, str],
) -> Union[str, CommentedSeq, CommentedMap]:
) -> Union[str, CommentedSeq, CommentedMap, None]:
if field in self.nolinkcheck:
return link
if isinstance(link, str):
Expand Down Expand Up @@ -1115,9 +1109,7 @@ def validate_link(
return link
else:
raise ValidationException(
"`{}` field is {}, expected string, list, or a dict.".format(
field, type(link).__name__
)
f"{field!r} field is {type(link).__name__}, expected string, list, or a dict."
)
return link

Expand Down
4 changes: 2 additions & 2 deletions schema_salad/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_error_message4() -> None:
match = r"""
^.+test4\.cwl:5:1: checking field\s+`outputs`
.+test4\.cwl:6:3: checking object\s+`.+test4\.cwl#bar`
\s+`type`\s+field\s+is\s+int,\s+expected\s+string,\s+list,\s+or\s+a\s+dict.$"""[
\s+'type'\s+field\s+is\s+int,\s+expected\s+string,\s+list,\s+or\s+a\s+dict.$"""[
1:
]
with pytest.raises(ValidationException, match=match):
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_error_message9() -> None:
match = (
r"""^.+test9\.cwl:7:1: checking field\s+`steps`
.+test9\.cwl:8:3: checking object\s+`.+test9\.cwl#step1`
.+test9\.cwl:9:5: `scatterMethod`\s+field\s+is\s+"""
.+test9\.cwl:9:5: 'scatterMethod'\s+field\s+is\s+"""
+ r"""int,\s+expected\s+string,\s+list,\s+or a\s+dict.$"""
)
with pytest.raises(ValidationException, match=match):
Expand Down

0 comments on commit 9318b72

Please sign in to comment.