pydantic-pycharm-plugin icon indicating copy to clipboard operation
pydantic-pycharm-plugin copied to clipboard

Optional Field wrongly report 'Parameter x unfilled'

Open Andrioden opened this issue 3 years ago • 3 comments
trafficstars

To Reproduce

class Test(BaseModel):
    name: Optional[str] = Field()


def test_model() -> None:
    a = Test()
    print(a.name)  # output None

Expected behavior No warning

Screenshots image

Environments:

  • IDE: PyCharm 2022.1 (Professional Edition)
  • OS: Ubuntu 21.10
  • Pydantic Version: 1.9.0
  • Plugin version: 0.3.12

Andrioden avatar Apr 20 '22 22:04 Andrioden

I'm also getting this issue, using python 3.10: Screen Shot 2022-05-16 at 6 58 15 PM

josecsotomorales avatar May 16 '22 21:05 josecsotomorales

Any ideas on this false positive? Seems like it affects multiple python versions

josecsotomorales avatar May 27 '22 20:05 josecsotomorales

I am also having the same issue on a "related" use-case, not exactly the same as above. In my case I am using metaclass to dynamically change the fields to Optional. When I do this PyCharm reports the same "unfulfilled" argument.

metaclass approach lifted from this SO: https://stackoverflow.com/questions/67699451/make-every-fields-as-optional-with-pydantic

class MetaMeasurement(pydantic.main.ModelMetaclass):
    def __new__(mcs, name, bases, namespaces, **kwargs):
        annotations = namespaces.get("__annotations__", {})
        for base in bases:
            annotations.update(base.__annotations__)

        for field in annotations:
            if not field.startswith("__"):
                annotations[field] = Optional[annotations[field]]

        namespaces["__annotations__"] = annotations
        return super().__new__(mcs, name, bases, namespaces, **kwargs)

If this is not the same issue to report here, I could create a new issue specifically for the metaclass case. Please let me know and I'll proceed accordingly.

Thank you so much for your time and efforts working on this plugin - it is really amazing! :heart_eyes: :heart_eyes: :heart_eyes:

jeremyschulman avatar Jun 25 '22 16:06 jeremyschulman

@Andrioden @josecsotomorales I'm sorry for my too-late reply. The fixed version has been released. Thank you very much.

koxudaxi avatar Dec 16 '22 14:12 koxudaxi

@jeremyschulman I'm sorry for my too-late reply. Could you please open another issue for your problem? I will fix it.

koxudaxi avatar Dec 16 '22 15:12 koxudaxi

Tested it now, works, thanks! Closing.

Andrioden avatar Dec 16 '22 15:12 Andrioden

thanks for the fix @koxudaxi! 🚀

josecsotomorales avatar Dec 16 '22 17:12 josecsotomorales