vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.23k stars 429 forks source link

[BUG] Typing error with Field q kwarg #1037

Open jam13 opened 10 months ago

jam13 commented 10 months ago

Describe the bug Filtering docs (https://django-ninja.dev/guides/input/filtering/) describe passing q string to Field definition:

class BookFilterSchema(FilterSchema):
    name: Optional[str] = Field(None, q='name__icontains') 

I had this working fine, but following package updates (including mypy to 1.7) it is failing type checking:

src/foo.py:23: error: Unexpected keyword argument "q" for "Field"  [call-arg]
/opt/venv/lib/python3.11/site-packages/pydantic/fields.py:673: note: "Field" defined here
Found 1 error in 1 file (checked 123 source files)

Versions (please complete the following information):

OtherBarry commented 10 months ago

Are you importing Field from ninja or pydantic?

jam13 commented 10 months ago

Are you importing Field from ninja or pydantic?

ninja

from ninja import Field, FilterSchema

class FooFilterSchema(FilterSchema):
    project_id: Optional[UUID] = Field(q="project_id")
vitalik commented 10 months ago

@jam13

that's weird - as you can see in tests it's used just fine:

https://github.com/vitalik/django-ninja/blob/master/tests/test_filter_schema.py

maybe you mixed some imports ? May be you forced deprecation warnings to throw errors (warnings.simplefilter) ?

jam13 commented 10 months ago

@jam13

that's weird - as you can see in tests it's used just fine:

https://github.com/vitalik/django-ninja/blob/master/tests/test_filter_schema.py

maybe you mixed some imports ? May be you forced deprecation warnings to throw errors (warnings.simplefilter) ?

I'll check it again and see if it's still an issue.

jam13 commented 10 months ago

@jam13 that's weird - as you can see in tests it's used just fine: https://github.com/vitalik/django-ninja/blob/master/tests/test_filter_schema.py maybe you mixed some imports ? May be you forced deprecation warnings to throw errors (warnings.simplefilter) ?

I'll check it again and see if it's still an issue.

Still getting the same error.

I've checked and I'm not importing Field directly from pydantic anywhere in the code.

Not knowingly forced deprecation warnings to throw errors.

My mypy config is:

[tool.mypy]
python_version = "3.11"
check_untyped_defs = true
disallow_any_generics = true
#disallow_any_unimported = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
#disallow_untyped_defs = true
ignore_missing_imports = true
no_implicit_optional = true
#show_error_codes = true
strict_equality = true
warn_redundant_casts = true
#warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
exclude = [
    "src/v1/migrations",
]
plugins = [
    "mypy_drf_plugin.main",
    "mypy_django_plugin.main",
    "pydantic.mypy",
    "sqlalchemy.ext.mypy.plugin",
]

Maybe it's a result of the pydantic plugin.

aristidebm commented 9 months ago

Describe the bug Filtering docs (https://django-ninja.dev/guides/input/filtering/) describe passing q string to Field definition:

class BookFilterSchema(FilterSchema):
    name: Optional[str] = Field(None, q='name__icontains') 

I had this working fine, but following package updates (including mypy to 1.7) it is failing type checking:

src/foo.py:23: error: Unexpected keyword argument "q" for "Field"  [call-arg]
/opt/venv/lib/python3.11/site-packages/pydantic/fields.py:673: note: "Field" defined here
Found 1 error in 1 file (checked 123 source files)

Versions (please complete the following information):

  • Python version: 3.11
  • Django version: 4.2
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.5.3

As your first error stated, you are importing Field form pydantic not from ninja

orsinium commented 9 months ago

I get the same error.

Are you importing Field from ninja or pydantic?

It doesn't matter if you import the field from pydantic or from ninja. The field is defined in the same place in both cases, django-ninja simply re-exports it:

https://github.com/vitalik/django-ninja/blob/master/ninja/__init__.py#L6

as you can see in tests it's used just fine:

Only tests/mypy_tests.py is type-checked. The rest of the test files are not:

https://github.com/vitalik/django-ninja/blob/master/.github/workflows/test_full.yml#L73

If you run mypy on the linked file, you get lots of type errors, including the one described in the issue:

$ mypy tests/test_filter_schema.py
...
tests/test_filter_schema.py:32:34: error: Unexpected keyword argument "q" for "Field"  [call-arg]
...
yshalsager commented 8 months ago

I am having the same problem, here are my versions:

Django==5.0.1
django-ninja==1.1.0
mypy==1.7.1
mypy-extensions==1.0.0
pydantic==2.5.3
pydantic_core==2.14.6
devxoul commented 6 months ago

Same here. VSCode doesn't complain about it but mypy CLI does. Field imported from ninja. What a weird behavior...

orsinium commented 6 months ago

I forgot to drop a message here (thank you @devxoul for raising the topic!) but I found a workaround. This works exactly the same in runtime and passes mypy checks (though doesn't look that nice):

class BookFilterSchema(FilterSchema):
    name: Optional[str] = Field(None, json_schema_extra={'q': 'name__icontains'})