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

Remove strict checking for validating tuples #8

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# autogenerated by sphinx-napoleon
docs/source/phir.rst

### Add custom ignores above this line ###

Expand Down
29 changes: 29 additions & 0 deletions docs/source/phir.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
phir package
============

Submodules
----------

phir.cli module
---------------

.. automodule:: phir.cli
:members:
:undoc-members:
:show-inheritance:

phir.model module
-----------------

.. automodule:: phir.model
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: phir
:members:
:undoc-members:
:show-inheritance:
5 changes: 4 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = (?x)(
disallow_any_unimported = true
disallow_any_expr = true
disallow_any_decorated = true
; disallow_any_explicit = true
disallow_any_explicit = true
disallow_any_generics = true
disallow_subclassing_any = true

Expand Down Expand Up @@ -50,6 +50,9 @@ warn_unused_configs = true
[mypy-tests.*]
disallow_untyped_defs = false

[mypy-phir.model]
disallow_any_explicit = false

[pydantic-mypy]
init_forbid_extra = true
init_typed = true
Expand Down
2 changes: 1 addition & 1 deletion phir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main() -> None:
data = json.load(f)

try:
print(PHIRModel.model_validate(data, strict=True))
print(PHIRModel.model_validate(data))
except ValidationError as e:
print(e)

Expand Down
10 changes: 5 additions & 5 deletions phir/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from __future__ import annotations

import abc
from typing import Annotated, Any, Literal, TypeAlias
from typing import Any, Literal, NewType, TypeAlias

from pydantic import BaseModel, ConfigDict, Field, PositiveInt
from pydantic import BaseModel, ConfigDict, Field, NonNegativeInt, PositiveInt

Idx: TypeAlias = Annotated[int, Field(strict=True, ge=0)]
Sym: TypeAlias = str
Bit: TypeAlias = Annotated[list[Sym | Idx], Field(max_length=2)]
Idx = NewType("Idx", NonNegativeInt)
Bit = NewType("Bit", tuple[Sym, Idx])
Copy link
Member Author

@qartik qartik Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qciaran, @ss2165 turns out tuple validation is supported by pydantic, but at the cost of strict validation. 💯


# Data Management

Expand Down Expand Up @@ -86,7 +86,7 @@ class COp(Op):


class FFCall(COp):
"""External Classical Function Call."""
"""External Classical (Foreign) Function Call."""

cop: Literal["ffcall"]
function: str
Expand Down
145 changes: 69 additions & 76 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
}
]
Expand All @@ -67,18 +66,17 @@
"$ref": "#/$defs/COp"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
}
]
Expand Down Expand Up @@ -212,7 +210,7 @@
},
"FFCall": {
"additionalProperties": false,
"description": "External Classical Function Call.",
"description": "External Classical (Foreign) Function Call.",
"properties": {
"metadata": {
"anyOf": [
Expand All @@ -239,18 +237,17 @@
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
}
]
Expand All @@ -277,18 +274,17 @@
"$ref": "#/$defs/COp"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
}
]
Expand Down Expand Up @@ -440,18 +436,17 @@
"anyOf": [
{
"items": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
},
"type": "array"
Expand All @@ -466,35 +461,33 @@
"args": {
"items": {
"anyOf": [
{
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
],
"type": "array"
},
{
"items": {
"anyOf": [
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
},
{
"items": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
],
"type": "array"
},
"type": "array"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_spec_example() -> None: # noqa: D103
with Path("tests/example.json").open() as f:
data = json.load(f) # type: ignore [misc]

PHIRModel.model_validate(data, strict=True) # type: ignore [misc]
PHIRModel.model_validate(data) # type: ignore [misc]