From bb3233d6b529e83a96081bd87d7234e96c6493aa Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Mon, 9 Dec 2024 15:36:35 -0500 Subject: [PATCH] Remove legacy code, fix #852 --- pydoctor/astbuilder.py | 5 +--- pydoctor/astutils.py | 53 +++++++----------------------------------- pydoctor/model.py | 7 ------ 3 files changed, 10 insertions(+), 55 deletions(-) diff --git a/pydoctor/astbuilder.py b/pydoctor/astbuilder.py index d350aa908..f80acdcc0 100644 --- a/pydoctor/astbuilder.py +++ b/pydoctor/astbuilder.py @@ -26,10 +26,7 @@ def parseFile(path: Path) -> ast.Module: src = f.read() + b'\n' return _parse(src, filename=str(path)) -if sys.version_info >= (3,8): - _parse = partial(ast.parse, type_comments=True) -else: - _parse = ast.parse +_parse = partial(ast.parse, type_comments=True) def _maybeAttribute(cls: model.Class, name: str) -> bool: """Check whether a name is a potential attribute of the given class. diff --git a/pydoctor/astutils.py b/pydoctor/astutils.py index d8e14f1b7..2163c841b 100644 --- a/pydoctor/astutils.py +++ b/pydoctor/astutils.py @@ -4,7 +4,6 @@ from __future__ import annotations import inspect -import platform import sys from numbers import Number from typing import Any, Callable, Collection, Iterator, Optional, List, Iterable, Sequence, TYPE_CHECKING, Tuple, Union, cast @@ -232,11 +231,7 @@ def get_assign_docstring_node(assign:ast.Assign | ast.AnnAssign) -> Str | None: def is_none_literal(node: ast.expr) -> bool: """Does this AST node represent the literal constant None?""" - if sys.version_info >= (3,8): - return isinstance(node, ast.Constant) and node.value is None - else: - # TODO: remove me when python3.7 is not supported anymore - return isinstance(node, (ast.Constant, ast.NameConstant)) and node.value is None + return isinstance(node, ast.Constant) and node.value is None def unstring_annotation(node: ast.expr, ctx:'model.Documentable', section:str='annotation') -> ast.expr: """Replace all strings in the given expression by parsed versions. @@ -489,23 +484,11 @@ def get_docstring_node(node: ast.AST) -> Str | None: return node.value return None -_string_lineno_is_end = sys.version_info < (3,8) \ - and platform.python_implementation() != 'PyPy' -"""True iff the 'lineno' attribute of an AST string node points to the last -line in the string, rather than the first line. -""" - - class _StrMeta(type): - if sys.version_info >= (3,8): - def __instancecheck__(self, instance: object) -> bool: - if isinstance(instance, ast.expr): - return get_str_value(instance) is not None - return False - else: - # TODO: remove me when python3.7 is not supported - def __instancecheck__(self, instance: object) -> bool: - return isinstance(instance, ast.Str) + def __instancecheck__(self, instance: object) -> bool: + if isinstance(instance, ast.expr): + return get_str_value(instance) is not None + return False class Str(ast.expr, metaclass=_StrMeta): """ @@ -514,15 +497,11 @@ class Str(ast.expr, metaclass=_StrMeta): Do not try to instanciate this class. """ + value: str + def __init__(self, *args: Any, **kwargs: Any) -> None: raise TypeError(f'{Str.__qualname__} cannot be instanciated') - if sys.version_info >= (3,8): - value: str - else: - # TODO: remove me when python3.7 is not supported - s: str - def extract_docstring_linenum(node: Str) -> int: r""" In older CPython versions, the AST only tells us the end line @@ -533,18 +512,8 @@ def extract_docstring_linenum(node: Str) -> int: Leading blank lines are stripped by cleandoc(), so we must return the line number of the first non-blank line. """ - if sys.version_info >= (3,8): - doc = node.value - else: - # TODO: remove me when python3.7 is not supported - doc = node.s + doc = node.value lineno = node.lineno - if _string_lineno_is_end: - # In older CPython versions, the AST only tells us the end line - # number and we must approximate the start line number. - # This approximation is correct if the docstring does not contain - # explicit newlines ('\n') or joined lines ('\' at end of line). - lineno -= doc.count('\n') # Leading blank lines are stripped by cleandoc(), so we must # return the line number of the first non-blank line. @@ -564,11 +533,7 @@ def extract_docstring(node: Str) -> Tuple[int, str]: - The line number of the first non-blank line of the docsring. See L{extract_docstring_linenum}. - The docstring to be parsed, cleaned by L{inspect.cleandoc}. """ - if sys.version_info >= (3,8): - value = node.value - else: - # TODO: remove me when python3.7 is not supported - value = node.s + value = node.value lineno = extract_docstring_linenum(node) return lineno, inspect.cleandoc(value) diff --git a/pydoctor/model.py b/pydoctor/model.py index 9c78fc3d0..fceaf21d9 100644 --- a/pydoctor/model.py +++ b/pydoctor/model.py @@ -13,7 +13,6 @@ from collections import defaultdict import datetime import importlib -import platform import sys import textwrap import types @@ -55,12 +54,6 @@ # Functions can't contain anything. -_string_lineno_is_end = sys.version_info < (3,8) \ - and platform.python_implementation() != 'PyPy' -"""True iff the 'lineno' attribute of an AST string node points to the last -line in the string, rather than the first line. -""" - class LineFromAst(int): "Simple L{int} wrapper for linenumbers coming from ast analysis."