Repository: pydantic/pydantic

Issue:
AnyUrl doesn't accept file URLs with implicit host
# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /shared/conda/envs/ubide-mastr/lib/python3.6/site-packages/pydantic
               python version: 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)  [GCC 7.3.0]
                     platform: Linux-5.4.0-48-generic-x86_64-with-debian-buster-sid
     optional deps. installed: []
```
```py
import pydantic

class MyModel(pydantic.BaseModel):
    url: pydantic.AnyUrl

MyModel(url='file:///foo/bar')
```
raises:
```
ValidationError: 1 validation error for MyModel
url
  URL host invalid (type=value_error.url.host)
```
However, `MyModel(url='file://hostname/foo/bar')`, i.e. a URL with an explicit hostname, works.

RFC 8089 [says](https://tools.ietf.org/html/rfc8089#section-2) that the hostname is optional. Hence I would expect that `AnyUrl` would accept that.


Discussion:
I guess we could add `host_required` like `user_required`, then make it true for most of the descendant types.

Resolving patch:
diff --git a/pydantic/__init__.py b/pydantic/__init__.py
--- a/pydantic/__init__.py
+++ b/pydantic/__init__.py
@@ -48,6 +48,7 @@
     # network
     'AnyUrl',
     'AnyHttpUrl',
+    'FileUrl',
     'HttpUrl',
     'stricturl',
     'EmailStr',
diff --git a/pydantic/networks.py b/pydantic/networks.py
--- a/pydantic/networks.py
+++ b/pydantic/networks.py
@@ -60,6 +60,7 @@ class Parts(TypedDict, total=False):
 __all__ = [
     'AnyUrl',
     'AnyHttpUrl',
+    'FileUrl',
     'HttpUrl',
     'stricturl',
     'EmailStr',
@@ -125,6 +126,7 @@ class AnyUrl(str):
     allowed_schemes: Optional[Set[str]] = None
     tld_required: bool = False
     user_required: bool = False
+    host_required: bool = True
     hidden_parts: Set[str] = set()
 
     __slots__ = ('scheme', 'user', 'password', 'host', 'tld', 'host_type', 'port', 'path', 'query', 'fragment')
@@ -140,7 +142,7 @@ def __init__(
         scheme: str,
         user: Optional[str] = None,
         password: Optional[str] = None,
-        host: str,
+        host: Optional[str] = None,
         tld: Optional[str] = None,
         host_type: str = 'domain',
         port: Optional[str] = None,
@@ -270,7 +272,8 @@ def validate_host(cls, parts: 'Parts') -> Tuple[str, Optional[str], str, bool]:
                 break
 
         if host is None:
-            raise errors.UrlHostError()
+            if cls.host_required:
+                raise errors.UrlHostError()
         elif host_type == 'domain':
             is_international = False
             d = ascii_domain_regex().fullmatch(host)
@@ -340,6 +343,11 @@ def hide_parts(cls, original_parts: 'Parts') -> None:
             cls.hidden_parts.add('port')
 
 
+class FileUrl(AnyUrl):
+    allowed_schemes = {'file'}
+    host_required = False
+
+
 class PostgresDsn(AnyUrl):
     allowed_schemes = {
         'postgres',
@@ -356,6 +364,7 @@ class PostgresDsn(AnyUrl):
 
 class RedisDsn(AnyUrl):
     allowed_schemes = {'redis', 'rediss'}
+    host_required = False
 
     @staticmethod
     def get_default_parts(parts: 'Parts') -> 'Parts':
@@ -383,6 +392,7 @@ def stricturl(
     min_length: int = 1,
     max_length: int = 2 ** 16,
     tld_required: bool = True,
+    host_required: bool = True,
     allowed_schemes: Optional[Union[FrozenSet[str], Set[str]]] = None,
 ) -> Type[AnyUrl]:
     # use kwargs then define conf in a dict to aid with IDE type hinting
@@ -391,6 +401,7 @@ def stricturl(
         min_length=min_length,
         max_length=max_length,
         tld_required=tld_required,
+        host_required=host_required,
         allowed_schemes=allowed_schemes,
     )
     return type('UrlValue', (AnyUrl,), namespace)


Test patch:
diff --git a/tests/test_networks.py b/tests/test_networks.py
--- a/tests/test_networks.py
+++ b/tests/test_networks.py
@@ -4,6 +4,7 @@
     AnyUrl,
     BaseModel,
     EmailStr,
+    FileUrl,
     HttpUrl,
     KafkaDsn,
     NameEmail,
@@ -69,6 +70,7 @@
         'http://twitter.com/@handle/',
         'http://11.11.11.11.example.com/action',
         'http://abc.11.11.11.11.example.com/action',
+        'file://localhost/foo/bar',
     ],
 )
 def test_any_url_success(value):
@@ -113,6 +115,7 @@ class Model(BaseModel):
         ),
         ('http://[192.168.1.1]:8329', 'value_error.url.host', 'URL host invalid', None),
         ('http://example.com:99999', 'value_error.url.port', 'URL port invalid, port cannot exceed 65535', None),
+        ('file:///foo/bar', 'value_error.url.host', 'URL host invalid', None),
     ],
 )
 def test_any_url_invalid(value, err_type, err_msg, err_ctx):
@@ -265,7 +268,7 @@ def test_http_url_success(value):
     class Model(BaseModel):
         v: HttpUrl
 
-    assert Model(v=value).v == value, value
+    assert Model(v=value).v == value
 
 
 @pytest.mark.parametrize(
@@ -343,6 +346,17 @@ class Model(BaseModel):
     assert Model(v=input).v.tld == output
 
 
+@pytest.mark.parametrize(
+    'value',
+    ['file:///foo/bar', 'file://localhost/foo/bar' 'file:////localhost/foo/bar'],
+)
+def test_file_url_success(value):
+    class Model(BaseModel):
+        v: FileUrl
+
+    assert Model(v=value).v == value
+
+
 def test_get_default_parts():
     class MyConnectionString(AnyUrl):
         @staticmethod
@@ -403,6 +417,11 @@ class Model(BaseModel):
     error = exc_info.value.errors()[0]
     assert error == {'loc': ('a',), 'msg': 'userinfo required in URL but missing', 'type': 'value_error.url.userinfo'}
 
+    with pytest.raises(ValidationError) as exc_info:
+        Model(a='postgres://user@/foo/bar:5432/app')
+    error = exc_info.value.errors()[0]
+    assert error == {'loc': ('a',), 'msg': 'URL host invalid', 'type': 'value_error.url.host'}
+
 
 def test_redis_dsns():
     class Model(BaseModel):
@@ -464,7 +483,11 @@ def test_custom_schemes():
     class Model(BaseModel):
         v: stricturl(strip_whitespace=False, allowed_schemes={'ws', 'wss'})  # noqa: F821
 
+    class Model2(BaseModel):
+        v: stricturl(host_required=False, allowed_schemes={'foo'})  # noqa: F821
+
     assert Model(v='ws://example.org').v == 'ws://example.org'
+    assert Model2(v='foo:///foo/bar').v == 'foo:///foo/bar'
 
     with pytest.raises(ValidationError):
         Model(v='http://example.org')
@@ -472,6 +495,9 @@ class Model(BaseModel):
     with pytest.raises(ValidationError):
         Model(v='ws://example.org  ')
 
+    with pytest.raises(ValidationError):
+        Model(v='ws:///foo/bar')
+
 
 @pytest.mark.parametrize(
     'kwargs,expected',