Skip to content

bad-argument-type on Annotated pydantic fields with custom schema #2586

@psalvaggio

Description

@psalvaggio

Describe the Bug

When running pyrefly check on this file: https://github.com/psalvaggio/scientific_pydantic/blob/main/tests/numpy/test_dtype.py#L22

I expect line 22 to be OK, but it actually gives:

[Pyrefly] Argument `Literal['>i4']` is not assignable to parameter `dtype` with type `dtype[Any]` in function `DefaultModel.__init__`

Background:

This is a library I am working on that has a number of pydantic adapters for scientific types. The one I linked here is for numpy.dtype, but the issue is similar on a number of other ones.

The field is annotated like:

dtype: ty.Annotated[np.dtype, DTypeAdapter()]

where DTypeAdapter() make a custom schema for the np.dtype. This particular schema is a plain validator that takes Any and either converts to a dtype or raises an exception, resulting in a validation error.

I would expect pyrefly to take Any for dtype in the model constructor, but it instead is constraining to np.dtype.

I think if there is a custom type in an Annotated field that implements __get_pydantic_core_schema__, then pyrefly should probably be fairly lax on the constraints for the corresponding keyword argument.

Happy to discuss or provide more details.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABA6cROgOZOpwukAddEOq1GTMpgwMIAYyFg6uKhNJT0M2YQBWcXOgD6cWQAsYVVE1H1mAKT3oAyqfOoAaqigBXGAqUrJaTkDWXoYKxobJlC6GCMXCyERMH5CABUATQAFAFEDAGEACRz8gGkASQA5AHFEISYGiLFmdC8aUh4%2BdGIk4XRZKF4%2BABE0thgAQSliBhg6OvRGpgEQFZWstSDZHmnZuiYweiZW9sJMVmJfPqWAJS8NanCAN08fRGO2sjOLq6XbRwA8pUmLFiLE4DANKgZPp3udxoQ4Ax9gAKGCETiEJgAcgAPGAABzYgA0OIAPhAACzYgCU9Uaa1Wq2ujQAAgMhlQYAwTLhMPSGpgYCkDAZONyDIEHrIQmF4mYLKKUQKltEoHBiSqlkZcF46LI4j93j8ANqsQgTdCkAC6msWqoaBhMGEwsHmqnUmkI1W5%2BTCzgVqEKLrddqWNKYAFoAHzROXGQOEP2xAOuBYO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2BRAJ5UOqBJwQCgQAMWgDACgaAsATyjhgoAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedLarger than "good first issue", but still well-defined and ready for someone to pick uppydanticIssues related to support for Pydantic

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions