dandi / dandi-schema

Schemata for DANDI archive project
Apache License 2.0
7 stars 10 forks source link

Move pattern specification for `BaseType.identifier` to the annotation for the `str` type #247

Closed candleindark closed 3 months ago

candleindark commented 4 months ago

My guess of the intention for BaseType.identifier is an object either validates to an AnyHttpUrl or a str matching the pattern of r"^[a-zA-Z0-9-]+:[a-zA-Z0-9-/\._]+$". If my guess is correct, the type annotation should be corrected to the proposed change in this PR.

The current type annotation of BaseType.identifier has problems exhibited by the Foo.identifier from the following example (Baz.identifier exhibits the solution proposed by this PR).

from typing import Optional, Union
import json

from typing_extensions import Annotated

from pydantic import BaseModel, Field, AnyHttpUrl, StringConstraints, ValidationError

PATTERN = r"^[a-zA-Z0-9-]+:[a-zA-Z0-9-/\._]+$"

class Foo(BaseModel):
    identifier: Optional[Union[AnyHttpUrl, str]] = Field(None, pattern=PATTERN)

class Bar(BaseModel):
    identifier: Optional[
        Union[AnyHttpUrl, Annotated[str, StringConstraints(pattern=PATTERN)]]
    ] = None

class Baz(BaseModel):
    identifier: Optional[
        Annotated[
            Union[AnyHttpUrl, Annotated[str, StringConstraints(pattern=PATTERN)]],
            Field(union_mode="left_to_right"),
        ]
    ] = None

# Try inputting an AnyHttpUrl object
print("======= Interation 0 =======")
try:
    foo0 = Foo(identifier=AnyHttpUrl("https://example.com"))
except ValidationError as e:
    print(e)
    """
    1 validation error for Foo
    identifier
      Input should be a valid string [type=string_type, input_value=Url('https://example.com/'), input_type=Url]
        For further information visit https://errors.pydantic.dev/2.7/v/string_type
    """
else:
    raise RuntimeError("Expected a validation error")

bar0 = Bar(identifier=AnyHttpUrl("https://example.com"))
baz0 = Baz(identifier=AnyHttpUrl("https://example.com"))

# Try inputting a string object
print("\n======= Interation 1 =======")
try:
    foo1 = Foo(identifier="https://www.python.org/~guido?arg=1#frag")
except ValidationError as e:
    print(e)
    """
    1 validation error for Foo
    identifier
      String should match pattern '^[a-zA-Z0-9-]+:[a-zA-Z0-9-/\._]+$' [type=string_pattern_mismatch, input_value='https://www.python.org/~guido?arg=1#frag', input_type=str]
        For further information visit https://errors.pydantic.dev/2.7/v/string_pattern_mismatch
    """
else:
    raise RuntimeError("Expected a validation error")

bar1 = Bar(identifier="https://www.python.org/~guido?arg=1#frag")
print(f"type(bar1.identifier): {type(bar1.identifier)}")
"""type(bar1.identifier): <class 'pydantic_core._pydantic_core.Url'>"""

baz1 = Baz(identifier="https://www.python.org/~guido?arg=1#frag")
print(f"type(baz1.identifier): {type(baz1.identifier)}")
"""type(baz1.identifier): <class 'pydantic_core._pydantic_core.Url'>"""

# Try inputting a string object that are simple
print("\n======= Interation 2 =======")
bar2 = Bar(identifier="https://example.com")
print(f"type(bar2.identifier): {type(bar2.identifier)}")
"""type(bar2.identifier): <class 'str'>"""

baz2 = Baz(identifier="https://example.com")
print(f"type(baz2.identifier): {type(baz2.identifier)}")
"""type(baz2.identifier): <class 'pydantic_core._pydantic_core.Url'>"""

Essentially, the current annotation fails to validate any AnyHttpUrl object and any HTTP URL, as a str, that contains a special character.

codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 91.83%. Comparing base (be57e13) to head (2fdc912).

:exclamation: There is a different number of reports uploaded between BASE (be57e13) and HEAD (2fdc912). Click for more details.

HEAD has 121 uploads less than BASE | Flag | BASE (be57e13) | HEAD (2fdc912) | |------|------|------| |unittests|151|30|
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #247 +/- ## ========================================== - Coverage 97.74% 91.83% -5.91% ========================================== Files 16 16 Lines 1727 1727 ========================================== - Hits 1688 1586 -102 - Misses 39 141 +102 ``` | [Flag](https://app.codecov.io/gh/dandi/dandi-schema/pull/247/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dandi) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/dandi/dandi-schema/pull/247/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dandi) | `91.83% <100.00%> (-5.91%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dandi#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.