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

Dtype numpy #316

Merged
merged 3 commits into from
Oct 1, 2024
Merged

Dtype numpy #316

merged 3 commits into from
Oct 1, 2024

Conversation

evalott100
Copy link
Contributor

Replacing #315

@evalott100
Copy link
Contributor Author

@coretl @danielballan

Despite the fact pydantic will fail correctly for the list field here:

@pytest.mark.parametrize(
"dtype_numpy", [
"Z",
"i",
"i4",
"4i",
"i>4",
">i",
[("some_str", "Z")],
]
)

The schema will not, it's because for some reason pydantic is outputting prefixItems instead of just items here:

@evalott100
Copy link
Contributor Author

Inserting "items": false makes everything work, I'm not too sure why pydantic isn't doing this already.

                {
                            "items": {
                                "maxItems": 2,
                                "minItems": 2,
                                "prefixItems": [
                                    {
                                        "type": "string"
                                    },
                                    {
                                        "description": "A numpy dtype e.g `<U9`, `<f16`",
                                        "pattern": "[|<>][tbiufcmMOSUV][0-9]+",
                                        "type": "string"
                                    }
                                ],
                                "items": false,
                                "type": "array"
                            },
                            "type": "array"

@evalott100
Copy link
Contributor Author

evalott100 commented Sep 30, 2024

For now we'll get around this by adding

_ConstrainedDtypeNpStructure = Annotated[
    Tuple[str, _ConstrainedDtype], Field(json_schema_extra={"items": False})
]

But I'll make an issue on pydantic.

@evalott100
Copy link
Contributor Author

Okay I figured it out, we simply can't pass a tuple into the jsonschema... While it will do positional args differently, it requires the array to be a list... But then paradoxically to generate the correct schema which does have different constraints on prefixItems pydantic will need the type hint to be Tuple.

@evalott100
Copy link
Contributor Author

@tacaswell implemented this to allow for tuples, but it isn't working in this case

python-jsonschema/jsonschema#148 (comment)

@evalott100
Copy link
Contributor Author

Upgraded the validator and all worked as it should.

@evalott100 evalott100 force-pushed the dtype_numpy branch 2 times, most recently from aec192a to 0e3a785 Compare October 1, 2024 10:52
@evalott100 evalott100 merged commit 4252db7 into main Oct 1, 2024
14 checks passed
@evalott100 evalott100 deleted the dtype_numpy branch October 1, 2024 14:47
@coretl
Copy link
Contributor

coretl commented Oct 24, 2024

fixes #308

@coretl coretl mentioned this pull request Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants