Skip to content

AnyUrl does not recognize fragment without query string #2168

@andrewmwhite

Description

@andrewmwhite

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.7.3
            pydantic compiled: True
                 install path: /Users/andwhite/Projects/lipy-sso_trunk/build/lipy-sso/environments/development-venv/lib/python3.7/site-packages/pydantic
               python version: 3.7.7 (default, Mar 10 2020, 16:11:21)  [Clang 11.0.0 (clang-1100.0.33.12)]
                     platform: Darwin-19.6.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

RFC3986 indicates that a URI with a fragment but not a query is valid, but pydantic does not parse this correctly, instead treating the fragment as part of the path:

from pydantic import AnyUrl, BaseModel


class Model(BaseModel):
    v: AnyUrl


def test_url_fragment_without_query():
    url = Model(v='https://pydantic-docs.helpmanual.io/usage/types/#constrained-types').v
    assert url.query is None
    assert url.fragment == 'constrainted-types'

Pytest output:

=========================================== test session starts ===========================================
platform darwin -- Python 3.7.7, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/Users/andwhite/Projects/lipy-sso_trunk/lipy-sso/.hypothesis/examples')
rootdir: /Users/andwhite/Projects/lipy-sso_trunk/lipy-sso, inifile: setup.cfg
plugins: xdist-1.29.0, requests-mock-1.8.0, html-2.0.1, forked-0.2, lipy-config-base-21.0.8, hypothesis-4.40.0, metadata-1.8.0, cov-2.8.1, lipy-sso-2.1.0
collected 1 item                                                                                          

repro.py F                                                                                          [100%]

================================================ FAILURES =================================================
_____________________________________ test_url_fragment_without_query _____________________________________

    def test_url_fragment_without_query():
        url = Model(v='https://pydantic-docs.helpmanual.io/usage/types/#constrained-types').v
        assert url.query is None
>       assert url.fragment == 'constrainted-types'
E       AssertionError: assert None == 'constrainted-types'
E        +  where None = AnyUrl('https://pydantic-docs.helpmanual.io/usage/types/#constrained-types', scheme='https', host='pydantic-docs.helpmanual.io', tld='io', host_type='domain', path='/usage/types/#constrained-types').fragment

repro.py:11: AssertionError
============================================ 1 failed in 0.13s ============================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions