From e84ddc74d504b874c9589f1d31635b0175b54e30 Mon Sep 17 00:00:00 2001 From: Chris Bowdon Date: Wed, 20 May 2020 10:00:36 +0100 Subject: [PATCH] Address mypy type errors These are all situations where the type is potentially None but in practice the value will not be None. Where possible I've corrected the inferred type, and otherwise I've protected with asserts if there is a guard elsewhere against None values. --- src/tld/base.py | 7 ++++--- src/tld/utils.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/tld/base.py b/src/tld/base.py index cdafec9..123f1fb 100644 --- a/src/tld/base.py +++ b/src/tld/base.py @@ -1,5 +1,6 @@ from codecs import open as codecs_open from urllib.request import urlopen +from typing import Optional from .exceptions import ( TldIOError, @@ -17,9 +18,9 @@ class BaseTLDSourceParser(metaclass=Registry): """Base TLD source parser.""" - uid: str = None - source_url: str = None - local_path: str = None + uid: Optional[str] = None + source_url: str + local_path: str @classmethod def validate(cls): diff --git a/src/tld/utils.py b/src/tld/utils.py index 68e4abb..6465eef 100644 --- a/src/tld/utils.py +++ b/src/tld/utils.py @@ -200,7 +200,6 @@ def get_tld_names( ): return _tld_names - local_file = None try: # Load the TLD names file if isabs(cls.local_path): @@ -441,6 +440,9 @@ def get_fld( if domain_parts is None: return None + # This should be None when domain_parts is None + # but mypy isn't quite smart enough to figure that out yet + assert non_zero_i is not None if non_zero_i < 0: # hostname = tld return parsed_url.hostname @@ -497,6 +499,10 @@ def get_tld( if domain_parts is None: return None + # This should be None when domain_parts is None + # but mypy isn't quite smart enough to figure that out yet + assert non_zero_i is not None + if not as_object: if non_zero_i < 0: # hostname = tld @@ -507,6 +513,9 @@ def get_tld( # hostname = tld subdomain = "" domain = "" + # This is checked in process_url but the type is ambiguous (Optional[str]) + # so this assertion is just to satisfy mypy + assert parsed_url.hostname is not None, "No hostname in URL" _tld = parsed_url.hostname else: subdomain = ".".join(domain_parts[:non_zero_i-1])