Skip to content

Commit

Permalink
Address mypy type errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbowdon committed May 20, 2020
1 parent d422286 commit e84ddc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/tld/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from codecs import open as codecs_open
from urllib.request import urlopen
from typing import Optional

from .exceptions import (
TldIOError,
Expand All @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion src/tld/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down

0 comments on commit e84ddc7

Please sign in to comment.