ApeWorX / ethpm-types

Implementation of EIP-2678
Apache License 2.0
14 stars 8 forks source link

fix: issue where could not use FileUrl in a manifest #56

Closed antazoey closed 1 year ago

antazoey commented 1 year ago

What I did

For some reaosn, FileUrl format fails pydantic validation for AnyUrl Simple example:

from pydantic import FileUrl, AnyUrl, BaseModel
from typing import Union

class GenericModel(BaseModel):
    v: AnyUrl  # Failure case 

class FileModel(BaseModel):
    v: FileUrl

class DontCareModel(BaseModel):
    v: Union[AnyUrl, FileUrl]

value = 'file:///foo/bar'
try:
    # This don't work on files for some reason
    GenericModel(v=value)  # Fails
except Exception as err:
    print(err)

model = FileModel(v=value)
assert model.v == value

test_model = DontCareModel(v=value)
assert model.v == value
print("success!")

How I did it

Redefine as AnyUrl = Union[_AnyUrl, FileUrl]

How to verify it

Can now have file URL in manifest. See tests.

Checklist