Skip to content

Commit

Permalink
refactor: Drop Python 3.7 support
Browse files Browse the repository at this point in the history
- Drop support in pyproject.toml
- Remove Python 3.7 dependencies
- Remove Python 3.7 related code
- Enable Mypy warn unused (remove unused)
  • Loading branch information
pawamoy committed Jul 4, 2023
1 parent 0bab0c4 commit e4be30a
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 309 deletions.
3 changes: 1 addition & 2 deletions config/mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[mypy]
ignore_missing_imports = true
exclude = tests/fixtures/
# TODO: set to true once Python 3.7 is dropped
warn_unused_ignores = false
warn_unused_ignores = true
show_error_codes = true
2 changes: 1 addition & 1 deletion config/ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target-version = "py37"
target-version = "py38"
line-length = 132
exclude = [
"fixtures",
Expand Down
7 changes: 1 addition & 6 deletions duties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

import os
import sys
from importlib.metadata import version as pkgversion
from pathlib import Path
from typing import TYPE_CHECKING, Any

from duty import duty
from duty.callables import black, blacken_docs, coverage, lazy, mkdocs, mypy, pytest, ruff, safety

if sys.version_info < (3, 8):
from importlib_metadata import version as pkgversion
else:
from importlib.metadata import version as pkgversion


if TYPE_CHECKING:
from duty.context import Context

Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ to generate API documentation or find breaking changes in your API."""
authors = [{name = "Timothée Mazzucotelli", email = "pawamoy@pm.me"}]
license = {text = "ISC"}
readme = "README.md"
requires-python = ">=3.7"
keywords = []
requires-python = ">=3.8"
keywords = ["api", "signature", "breaking-changes", "static-analysis", "dynamic-analysis"]
dynamic = ["version"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand All @@ -32,7 +31,6 @@ classifiers = [
"Typing :: Typed",
]
dependencies = [
"cached-property; python_version < '3.8'",
"colorama>=0.4",
]

Expand Down
7 changes: 1 addition & 6 deletions scripts/gen_credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import re
import sys
from importlib.metadata import PackageNotFoundError, metadata
from itertools import chain
from pathlib import Path
from textwrap import dedent
Expand All @@ -13,11 +13,6 @@
from jinja2 import StrictUndefined
from jinja2.sandbox import SandboxedEnvironment

if sys.version_info < (3, 8):
from importlib_metadata import PackageNotFoundError, metadata
else:
from importlib.metadata import PackageNotFoundError, metadata

project_dir = Path(".")
pyproject = toml.load(project_dir / "pyproject.toml")
project = pyproject["project"]
Expand Down
6 changes: 1 addition & 5 deletions src/griffe/agents/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from __future__ import annotations

import ast
import sys
from inspect import Parameter as SignatureParameter
from inspect import Signature, cleandoc, getmodule, ismodule
from inspect import signature as getsignature
Expand Down Expand Up @@ -53,10 +52,7 @@
from griffe.expressions import Expression, Name


if sys.version_info < (3, 8):
from cached_property import cached_property
else:
from functools import cached_property
from functools import cached_property

empty = Signature.empty

Expand Down
114 changes: 12 additions & 102 deletions src/griffe/agents/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
from ast import Mod as NodeMod
from ast import Mult as NodeMult
from ast import Name as NodeName
from ast import NamedExpr as NodeNamedExpr
from ast import Not as NodeNot
from ast import NotEq as NodeNotEq
from ast import NotIn as NodeNotIn
from ast import Num as NodeNum
from ast import Or as NodeOr
from ast import Pow as NodePow
from ast import RShift as NodeRShift
Expand All @@ -72,26 +72,13 @@
from ast import comprehension as NodeComprehension
from ast import keyword as NodeKeyword
from contextlib import contextmanager, suppress
from functools import partial
from functools import cached_property, partial
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Iterator, Sequence

from griffe.exceptions import LastNodeError, RootNodeError
from griffe.expressions import Expression, Name
from griffe.logger import LogLevel, get_logger

# TODO: remove condition once Python 3.7 support is dropped
if sys.version_info >= (3, 8):
from ast import NamedExpr as NodeNamedExpr

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):
from ast import Bytes as NodeBytes
from ast import NameConstant as NodeNameConstant

from cached_property import cached_property
else:
from functools import cached_property

# TODO: remove once Python 3.8 support is dropped
if sys.version_info < (3, 9):
from ast import ExtSlice as NodeExtSlice
Expand All @@ -112,10 +99,6 @@ class ASTNode:

parent: ASTNode

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):
end_lineno = property(lambda _: None)

@cached_property
def kind(self) -> str:
"""Return the kind of this node.
Expand Down Expand Up @@ -544,16 +527,8 @@ def __init__(self, parent: Module) -> None:
NodeBinOp: self._extract_binop,
}

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):
self._node_map[NodeNameConstant] = self._extract_nameconstant # type: ignore[assignment]
self._node_map[NodeStr] = self._extract_str # type: ignore[assignment]

def _extract_constant(self, node: NodeConstant) -> list[str]:
try:
return [node.value]
except AttributeError:
return [node.s] # TODO: remove once Python 3.7 is dropped
return [node.value]

def _extract_name(self, node: NodeName) -> list[Name]:
return [Name(node.id, partial(self.parent.resolve, node.id))]
Expand All @@ -572,15 +547,6 @@ def _extract_binop(self, node: NodeBinOp) -> list[str | Name]:
right = self._extract(node.right)
return left + right

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):

def _extract_nameconstant(self, node: NodeNameConstant) -> list[Name]:
return [node.value]

def _extract_str(self, node: NodeStr) -> list[str]:
return [node.s]

def _extract(self, node: AST) -> list[str | Name]:
return self._node_map[type(node)](node)

Expand Down Expand Up @@ -621,7 +587,7 @@ def safe_get__all__(
except Exception as error: # noqa: BLE001
message = f"Failed to extract `__all__` value: {get_value(node.value)}"
with suppress(Exception):
message += f" at {parent.relative_filepath}:{node.lineno}" # type: ignore[union-attr]
message += f" at {parent.relative_filepath}:{node.lineno}"
if isinstance(error, KeyError):
message += f": unsupported node {error}"
else:
Expand Down Expand Up @@ -672,13 +638,6 @@ def __init__(self, parent: Module | Class, *, parse_strings: bool | None = None)
if sys.version_info < (3, 9):
self._node_map[NodeIndex] = self._build_index

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):
self._node_map[NodeBytes] = self._build_bytes
self._node_map[NodeNameConstant] = self._build_nameconstant
self._node_map[NodeNum] = self._build_num
self._node_map[NodeStr] = self._build_str

@contextmanager
def literal_strings(self) -> Iterator[None]:
self._literal_strings = True
Expand Down Expand Up @@ -798,22 +757,6 @@ def _build_usub(self, node: NodeUSub) -> str: # noqa: ARG002
def _build_index(self, node: NodeIndex) -> str | Name | Expression:
return self._build(node.value)

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):

def _build_bytes(self, node: NodeBytes) -> str:
return repr(node.s)

def _build_nameconstant(self, node: NodeNameConstant) -> str:
return repr(node.value)

def _build_num(self, node: NodeNum) -> str:
return repr(node.n)

def _build_str(self, node: NodeStr) -> str | Name:
node.value = node.s # type: ignore[attr-defined] # fake node as constant
return self._node_map[NodeConstant](node) # type: ignore[return-value]

def _build(self, node: AST) -> str | Name | Expression:
return self._node_map[type(node)](node)

Expand Down Expand Up @@ -922,16 +865,10 @@ def get_docstring(
else:
return None, None, None
if isinstance(doc, NodeConstant) and isinstance(doc.value, str):
return doc.value, doc.lineno, doc.end_lineno # type: ignore[attr-defined]
return doc.value, doc.lineno, doc.end_lineno
if isinstance(doc, NodeStr):
# TODO: remove once Python 3.7 support is dropped
# on Python 3.7, lineno seems to be the ending line of the string
# rather than the starting one, so we substract the number of newlines
lineno = doc.lineno
if sys.version_info < (3, 8):
lineno -= doc.s.count("\n")

return doc.s, lineno, doc.end_lineno # type: ignore[attr-defined]
return doc.s, lineno, doc.end_lineno
return None, None, None


Expand Down Expand Up @@ -1001,22 +938,13 @@ def __init__(self) -> None:
NodeYield: self._extract_yield,
}

# TODO: remove condition once Python 3.7 support is dropped
if sys.version_info >= (3, 8):
self._node_map[NodeNamedExpr] = self._extract_named_expr
self._node_map[NodeNamedExpr] = self._extract_named_expr

# TODO: remove once Python 3.8 support is dropped
if sys.version_info < (3, 9):
self._node_map[NodeExtSlice] = self._extract_extslice
self._node_map[NodeIndex] = self._extract_index

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):
self._node_map[NodeBytes] = self._extract_bytes
self._node_map[NodeNameConstant] = self._extract_nameconstant
self._node_map[NodeNum] = self._extract_num
self._node_map[NodeStr] = self._extract_str

def _extract_add(self, node: NodeAdd) -> str: # noqa: ARG002
return "+"

Expand Down Expand Up @@ -1237,13 +1165,10 @@ def _extract_yield(self, node: NodeYield) -> str:
return repr(None)
return self._extract(node.value)

# TODO: remove condition once Python 3.7 support is dropped
if sys.version_info >= (3, 8):

def _extract_named_expr(self, node: NodeNamedExpr) -> str:
return f"({self._extract(node.target)} := {self._extract(node.value)})"
def _extract_named_expr(self, node: NodeNamedExpr) -> str:
return f"({self._extract(node.target)} := {self._extract(node.value)})"

# TODO: remove once Python 3.8 support is dropped
# TODO: remove once Python 3.8 support is
if sys.version_info < (3, 9):

def _extract_extslice(self, node: NodeExtSlice) -> str:
Expand All @@ -1252,21 +1177,6 @@ def _extract_extslice(self, node: NodeExtSlice) -> str:
def _extract_index(self, node: NodeIndex) -> str:
return self._extract(node.value)

# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):

def _extract_bytes(self, node: NodeBytes) -> str:
return repr(node.s)

def _extract_nameconstant(self, node: NodeNameConstant) -> str:
return repr(node.value)

def _extract_num(self, node: NodeNum) -> str:
return repr(node.n)

def _extract_str(self, node: NodeStr) -> str:
return repr(node.s)

def _extract(self, node: AST) -> str:
return self._node_map[type(node)](node)

Expand Down Expand Up @@ -1394,8 +1304,8 @@ def get_parameter_default(node: AST | None, filepath: Path, lines_collection: Li
default = safe_get_value(node)
if default is not None:
return default
if node.lineno == node.end_lineno: # type: ignore[attr-defined]
return lines_collection[filepath][node.lineno - 1][node.col_offset : node.end_col_offset] # type: ignore[attr-defined]
if node.lineno == node.end_lineno:
return lines_collection[filepath][node.lineno - 1][node.col_offset : node.end_col_offset]
# TODO: handle multiple line defaults
return None

Expand Down
Loading

0 comments on commit e4be30a

Please sign in to comment.