Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the use of Pydantic type adapter #38

Open
candleindark opened this issue Dec 5, 2024 · 0 comments
Open

Simplify the use of Pydantic type adapter #38

candleindark opened this issue Dec 5, 2024 · 0 comments
Assignees

Comments

@candleindark
Copy link
Member

The use of Pydantic type adapters are quite unwieldy in the program at this point. It involves creation of a type adapter for each type that needed one but, for performance reason, one should avoid repeatedly creating adapters for the same type. Thus, for each type that currently needs a type adapter, an adapter is created at

DANDI_METADATA_ADAPTER = TypeAdapter(DandiMetadata)
PYDANTIC_VALIDATION_ERRS_ADAPTER = TypeAdapter(PydanticValidationErrsType)
LINKML_VALIDATION_ERRS_ADAPTER = TypeAdapter(LinkmlValidationErrsType)
DANDI_METADATA_LIST_ADAPTER = TypeAdapter(list[DandiMetadata])
DANDISET_PYDANTIC_REPORT_LIST_ADAPTER = TypeAdapter(list[DandisetValidationReport])
ASSET_PYDANTIC_REPORT_LIST_ADAPTER = TypeAdapter(list[AssetValidationReport])
.

This set up is not sustainable if more types need a type adapter. Create some class that stores adapters in a dictionary keyed by the type they are intended for.

The following snipped exhibit properties they are useful in implementing such a class.

snippet exhibiting properties useful in implementing such a class
from typing import TypeAlias

t1 = list[str]
t2 = list[int]
t3 = list[str]
t4: TypeAlias = list[str]
t5: TypeAlias = list[str]

print(f't1: {t1}')

if t1 == t2:
    print('t1 == t2')

if t1 == t3:
    print('t1 == t3')

if t1 == t4:
    print('t1 == t4')

if t4 == t5:
    print('t4 == t5')

if t1 == list[str]:
    print('t1 == list[str]')

print("=====")

print(f"type(t1): {type(t1)}")
print(f"type(t2): {type(t2)}")
print(f"type(t3): {type(t3)}")
print(f"type(t4): {type(t4)}")
print(f"type(t5): {type(t5)}")
print(f"type(list[str]): {type(list[str])}")

print("=====")
s = {t1, t2, t3, t4, t5, list[str]}
print(f"s: {s}")
@candleindark candleindark self-assigned this Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant