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

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

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

candleindark
Copy link
Member

@candleindark candleindark commented Jul 30, 2024

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.

To the annotation for the `str` type. The
previous specification is actually incorrect.
Copy link

codecov bot commented Jul 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

❗ 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
@@            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 Coverage Δ
unittests 91.83% <100.00%> (-5.91%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@candleindark candleindark added the bug Something isn't working label Jul 30, 2024
@yarikoptic yarikoptic added the release Create a release when this pr is merged label Aug 12, 2024
@yarikoptic yarikoptic merged commit 73e0630 into dandi:master Aug 12, 2024
45 of 46 checks passed
@candleindark candleindark deleted the pattern_in_annotated branch October 30, 2024 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working release Create a release when this pr is merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants