Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
MihanixA committed Mar 26, 2021
1 parent 8c1875a commit ba58ec7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions changes/2447-MihanixA.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
- added `get_default_parts` and `apply_default_parts` methods to `AnyUrl`
- `HttpUrl` now inherits `AnyHttpUrl` instead of `AnyUrl`
- `HttpUrl` now has default port 80 for http and 443 for https
- added `hidden_parts` field to hide parts from url representation
28 changes: 16 additions & 12 deletions pydantic/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def build(
if user or password:
url += '@'
url += host
if port and ('port' not in cls.hidden_parts):
if port and ('port' not in cls.hidden_parts): # type: ignore
url += ':' + port
if path:
url += path
Expand Down Expand Up @@ -214,7 +214,11 @@ def validate(cls, value: Any, field: 'ModelField', config: 'BaseConfig') -> 'Any
assert m, 'URL regex failed unexpectedly'

original_parts = cast('Parts', m.groupdict())
cls.hide_parts(original_parts)

cls.hidden_parts = cls.hidden_parts if cls.hidden_parts else set()
parts_to_hide = cls.hide_parts(original_parts)
cls.hidden_parts = cls.hidden_parts.union(parts_to_hide)

parts = cls.apply_default_parts(original_parts)
parts = cls.validate_parts(parts)

Expand Down Expand Up @@ -304,9 +308,9 @@ def validate_host(cls, parts: 'Parts') -> Tuple[str, Optional[str], str, bool]:
def get_default_parts(parts: 'Parts') -> 'Parts':
return {}

@classmethod
def hide_parts(cls, original_parts: 'Parts') -> None:
cls.hidden_parts = set()
@staticmethod
def hide_parts(original_parts: 'Parts') -> Set[str]:
return set()

@classmethod
def apply_default_parts(cls, parts: 'Parts') -> 'Parts':
Expand All @@ -329,15 +333,15 @@ class HttpUrl(AnyHttpUrl):
# https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
max_length = 2083

@staticmethod
def get_default_parts(parts: 'Parts') -> 'Parts':
return {'port': '80' if parts['scheme'] == 'http' else '443'}

@classmethod
def hide_parts(cls, original_parts: 'Parts') -> None:
super().hide_parts(original_parts)
def hide_parts(cls, original_parts: 'Parts') -> Set[str]:
if 'port' in original_parts:
cls.hidden_parts.add('port')
return {'port'}
return set()

@staticmethod
def get_default_parts(original_parts: 'Parts') -> 'Parts':
return {'port': '80' if original_parts['scheme'] == 'http' else '443'}


class PostgresDsn(AnyUrl):
Expand Down

0 comments on commit ba58ec7

Please sign in to comment.