Skip to content

Commit

Permalink
Switch from pycodestyle and Pylint to Ruff (john-kurkowski#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-kurkowski authored Sep 19, 2023
1 parent c92ad1f commit 03a45e2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 55 deletions.
25 changes: 22 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ disallow_untyped_calls = true
module = ["tldextract.*"]
disallow_untyped_defs = true

[tool.pylint.master]
disable = "fixme"
no-docstring-rgx = "(^_|test_.*)"
[tool.ruff]
select = [
"A",
"B",
"C",
"D",
"E",
"F",
"I",
"N",
"UP",
"W",
]
ignore = [
"E501", # line too long; if Black does its job, not worried about the rare long line
]

[tool.ruff.per-file-ignores]
"**/tests/**/*" = ["D103"]

[tool.ruff.pydocstyle]
convention = "pep257"
5 changes: 0 additions & 5 deletions setup.cfg

This file was deleted.

4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ def reset_log_level():
Generally want test output the Unix way: silence is golden.
"""
tldextract.cache._DID_LOG_UNABLE_TO_CACHE = ( # pylint: disable=protected-access
False
)
tldextract.cache._DID_LOG_UNABLE_TO_CACHE = False
logging.getLogger().setLevel(logging.WARN)
34 changes: 13 additions & 21 deletions tldextract/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

_DID_LOG_UNABLE_TO_CACHE = False

T = TypeVar("T") # pylint: disable=invalid-name
T = TypeVar("T")


def get_pkg_unique_identifier() -> str:
Expand All @@ -32,7 +32,6 @@ def get_pkg_unique_identifier() -> str:
a new version of tldextract
"""
try:
# pylint: disable=import-outside-toplevel
from tldextract._version import version
except ImportError:
version = "dev"
Expand Down Expand Up @@ -83,6 +82,7 @@ class DiskCache:
"""Disk _cache that only works for jsonable values."""

def __init__(self, cache_dir: str | None, lock_timeout: int = 20):
"""Construct a disk cache in the given directory."""
self.enabled = bool(cache_dir)
self.cache_dir = os.path.expanduser(str(cache_dir) or "")
self.lock_timeout = lock_timeout
Expand All @@ -99,14 +99,13 @@ def get(self, namespace: str, key: str | dict[str, Hashable]) -> object:
if not os.path.isfile(cache_filepath):
raise KeyError("namespace: " + namespace + " key: " + repr(key))
try:
# pylint: disable-next=unspecified-encoding
with open(cache_filepath) as cache_file:
return json.load(cache_file)
except (OSError, ValueError) as exc:
LOG.error("error reading TLD cache file %s: %s", cache_filepath, exc)
raise KeyError("namespace: " + namespace + " key: " + repr(key)) from None

def set(
def set( # noqa: A003
self, namespace: str, key: str | dict[str, Hashable], value: object
) -> None:
"""Set a value in the disk cache."""
Expand All @@ -117,19 +116,16 @@ def set(

try:
_make_dir(cache_filepath)
# pylint: disable-next=unspecified-encoding
with open(cache_filepath, "w") as cache_file:
json.dump(value, cache_file)
except OSError as ioe:
global _DID_LOG_UNABLE_TO_CACHE # pylint: disable=global-statement
global _DID_LOG_UNABLE_TO_CACHE
if not _DID_LOG_UNABLE_TO_CACHE:
LOG.warning(
(
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s"
),
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s",
namespace,
key,
cache_filepath,
Expand Down Expand Up @@ -181,15 +177,13 @@ def run_and_cache(
try:
_make_dir(cache_filepath)
except OSError as ioe:
global _DID_LOG_UNABLE_TO_CACHE # pylint: disable=global-statement
global _DID_LOG_UNABLE_TO_CACHE
if not _DID_LOG_UNABLE_TO_CACHE:
LOG.warning(
(
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s"
),
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s",
namespace,
key_args,
cache_filepath,
Expand All @@ -199,8 +193,6 @@ def run_and_cache(

return func(**kwargs)

# Disable lint of 3rd party (see also https://github.com/tox-dev/py-filelock/issues/102)
# pylint: disable-next=abstract-class-instantiated
with FileLock(lock_path, timeout=self.lock_timeout):
try:
result = cast(T, self.get(namespace=namespace, key=key_args))
Expand Down
2 changes: 1 addition & 1 deletion tldextract/suffix_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PUBLIC_PRIVATE_SUFFIX_SEPARATOR = "// ===BEGIN PRIVATE DOMAINS==="


class SuffixListNotFound(LookupError):
class SuffixListNotFound(LookupError): # noqa: N818
"""A recoverable error while looking up a suffix list.
Recoverable because you can specify backups, or use this library's bundled
Expand Down
16 changes: 6 additions & 10 deletions tldextract/tldextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@


class ExtractResult(NamedTuple):
"""namedtuple of a URL's subdomain, domain, suffix,
and flag that indicates if URL has private suffix."""
"""namedtuple of a URL's subdomain, domain, suffix, and flag that indicates if URL has private suffix."""

subdomain: str
domain: str
Expand Down Expand Up @@ -111,8 +110,6 @@ def fqdn(self) -> str:
''
"""
if self.suffix and (self.domain or self.is_private):
# Disable bogus lint error (https://github.com/PyCQA/pylint/issues/2568)
# pylint: disable-next=not-an-iterable,unsubscriptable-object
return ".".join(i for i in self[:3] if i)
return ""

Expand Down Expand Up @@ -164,8 +161,8 @@ def ipv6(self) -> str:
class TLDExtract:
"""A callable for extracting, subdomain, domain, and suffix components from a URL."""

# TODO: Agreed with Pylint: too-many-arguments
def __init__( # pylint: disable=too-many-arguments
# TODO: too-many-arguments
def __init__(
self,
cache_dir: str | None = get_cache_dir(),
suffix_list_urls: Sequence[str] = PUBLIC_SUFFIX_LIST_URLS,
Expand Down Expand Up @@ -371,6 +368,7 @@ class Trie:
def __init__(
self, matches: dict | None = None, end: bool = False, is_private: bool = False
) -> None:
"""TODO."""
self.matches = matches if matches else {}
self.end = end
self.is_private = is_private
Expand Down Expand Up @@ -411,16 +409,14 @@ def add_suffix(self, suffix: str, is_private: bool = False) -> None:


@wraps(TLD_EXTRACTOR.__call__)
def extract( # pylint: disable=missing-function-docstring
def extract( # noqa: D103
url: str, include_psl_private_domains: bool | None = False
) -> ExtractResult:
return TLD_EXTRACTOR(url, include_psl_private_domains=include_psl_private_domains)


@wraps(TLD_EXTRACTOR.update)
def update( # type: ignore[no-untyped-def]
*args, **kwargs
): # pylint: disable=missing-function-docstring
def update(*args, **kwargs): # type: ignore[no-untyped-def] # noqa: D103
return TLD_EXTRACTOR.update(*args, **kwargs)


Expand Down
15 changes: 3 additions & 12 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,31 @@ envlist = py{37,38,39,310,311,py3},codestyle,lint,typecheck

[testenv]
deps =
pylint
pytest
pytest-gitignore
pytest-mock
pytest-pylint
responses

commands = pytest --pylint {posargs}
commands = pytest {posargs}

[testenv:codestyle]
basepython = python3.7
deps =
black
pycodestyle
commands =
pycodestyle tldextract tests {posargs}
black --check {posargs:.}

[testenv:lint]
basepython = python3.7
deps =
pytest
pytest-gitignore
pytest-pylint
responses
commands = pytest --pylint -m pylint {posargs}
ruff
commands = ruff check {posargs:.}

[testenv:typecheck]
basepython = python3.7
deps =
mypy
pytest
pytest-gitignore
pytest-pylint
responses
types-filelock
types-requests
Expand Down

0 comments on commit 03a45e2

Please sign in to comment.