dflook / python-minifier

Transform Python source code into its most compact representation
MIT License
583 stars 43 forks source link

Python Minifier doesn't process pydantic code correctly. #45

Closed richard-scott closed 2 years ago

richard-scott commented 2 years ago

When trying to minify pydantic code and run it, this causes errors. When running the example available here I get the following output after minification.

$ python3 pydantic-example.py
Traceback (most recent call last):
  File "pydantic/validators.py", line 709, in pydantic.validators.find_validators
TypeError: issubclass() arg 1 must be a class

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/...pydantic-example.py", line 4, in <module>
    class User(BaseModel):id:0;name='John Doe';signup_ts=None;friends=[]
  File "pydantic/main.py", line 204, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/fields.py", line 488, in pydantic.fields.ModelField.infer
  File "pydantic/fields.py", line 419, in pydantic.fields.ModelField.__init__
  File "pydantic/fields.py", line 539, in pydantic.fields.ModelField.prepare
  File "pydantic/fields.py", line 801, in pydantic.fields.ModelField.populate_validators
  File "pydantic/validators.py", line 718, in find_validators
RuntimeError: error checking inheritance of 0 (type: int)
dflook commented 2 years ago

This is because python-minifier is removing annotations, which pydantic needs to work.

You can disable the removal of annotations by passing --no-remove-annotations to the pyminify command, or setting remove_annotations=False when calling python_minifier.minify()

richard-scott commented 2 years ago

Yes, that does the trick 👍