Skip to content

Commit

Permalink
Drop Python <=3.7 syntax
Browse files Browse the repository at this point in the history
Closes #449
  • Loading branch information
AWhetter committed Jun 22, 2024
1 parent 1aade0f commit 83560ba
Show file tree
Hide file tree
Showing 47 changed files with 68 additions and 111 deletions.
2 changes: 1 addition & 1 deletion autoapi/_astroid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get_full_basenames(node):
def _get_const_value(node):
if isinstance(node, astroid.nodes.Const):
if isinstance(node.value, str) and "\n" in node.value:
return '"""{0}"""'.format(node.value)
return f'"""{node.value}"""'

class NotConstException(Exception):
pass
Expand Down
2 changes: 1 addition & 1 deletion autoapi/_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def read_file(self, path, **kwargs):
else:
parsed_data = Parser().parse_file(path)
return parsed_data
except (IOError, TypeError, ImportError):
except (OSError, TypeError, ImportError):
LOGGER.debug("Reason:", exc_info=True)
LOGGER.warning(
f"Unable to read file: {path}",
Expand Down
29 changes: 14 additions & 15 deletions autoapi/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import functools
import pathlib
from typing import List, Optional, Tuple

import sphinx
import sphinx.util
Expand Down Expand Up @@ -66,7 +65,7 @@ def __init__(
This is the same as the fully qualified name of the object.
"""

self.children: List[PythonObject] = []
self.children: list[PythonObject] = []
"""The members of this object.
For example, the classes and functions defined in the parent module.
Expand All @@ -79,7 +78,7 @@ def __init__(

# For later
self._class_content = class_content
self._display_cache: Optional[bool] = None
self._display_cache: bool | None = None

def __getstate__(self):
"""Obtains serialisable data for pickling."""
Expand Down Expand Up @@ -248,7 +247,7 @@ def _ask_ignore(self, skip: bool) -> bool:

return ask_result if ask_result is not None else skip

def _children_of_type(self, type_: str) -> List[PythonObject]:
def _children_of_type(self, type_: str) -> list[PythonObject]:
return list(child for child in self.children if child.type == type_)


Expand All @@ -268,20 +267,20 @@ def __init__(self, *args, **kwargs):
self.args: str = _format_args(self.obj["args"], show_annotations)
"""The arguments to this object, formatted as a string."""

self.return_annotation: Optional[str] = (
self.return_annotation: str | None = (
self.obj["return_annotation"] if show_annotations else None
)
"""The type annotation for the return type of this function.
This will be ``None`` if an annotation
or annotation comment was not given.
"""
self.properties: List[str] = self.obj["properties"]
self.properties: list[str] = self.obj["properties"]
"""The properties that describe what type of function this is.
Can be only be: async.
"""
self.overloads: List[Tuple[str, str]] = [
self.overloads: list[tuple[str, str]] = [
(_format_args(args), return_annotation)
for args, return_annotation in self.obj["overloads"]
]
Expand All @@ -300,7 +299,7 @@ class PythonMethod(PythonFunction):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.properties: List[str] = self.obj["properties"]
self.properties: list[str] = self.obj["properties"]
"""The properties that describe what type of method this is.
Can be any of: abstractmethod, async, classmethod, property, staticmethod.
Expand All @@ -322,9 +321,9 @@ class PythonProperty(PythonObject):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.annotation: Optional[str] = self.obj["return_annotation"]
self.annotation: str | None = self.obj["return_annotation"]
"""The type annotation of this property."""
self.properties: List[str] = self.obj["properties"]
self.properties: list[str] = self.obj["properties"]
"""The properties that describe what type of property this is.
Can be any of: abstractmethod, classmethod.
Expand All @@ -340,12 +339,12 @@ class PythonData(PythonObject):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.value: Optional[str] = self.obj.get("value")
self.value: str | None = self.obj.get("value")
"""The value of this attribute.
This will be ``None`` if the value is not constant.
"""
self.annotation: Optional[str] = self.obj.get("annotation")
self.annotation: str | None = self.obj.get("annotation")
"""The type annotation of this attribute.
This will be ``None`` if an annotation
Expand Down Expand Up @@ -424,7 +423,7 @@ class PythonClass(PythonObject):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.bases: List[str] = self.obj["bases"]
self.bases: list[str] = self.obj["bases"]
"""The fully qualified names of all base classes."""

self._docstring_resolved: bool = False
Expand All @@ -447,7 +446,7 @@ def args(self) -> str:
return args

@property
def overloads(self) -> List[Tuple[str, str]]:
def overloads(self) -> list[tuple[str, str]]:
overloads = []

if self.constructor:
Expand Down Expand Up @@ -503,7 +502,7 @@ def classes(self):
return self._children_of_type("class")

@property
@functools.lru_cache()
@functools.lru_cache
def constructor(self):
for child in self.children:
if child.short_name == "__init__":
Expand Down
3 changes: 1 addition & 2 deletions autoapi/documenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def get_doc(self, encoding=None, ignore=1):

def process_doc(self, docstrings):
for docstring in docstrings:
for line in docstring:
yield line
yield from docstring

yield ""

Expand Down
6 changes: 1 addition & 5 deletions autoapi/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This extension allows you to automagically generate API documentation from your project.
"""

import io
import os
import shutil
from typing import Dict, Tuple
Expand Down Expand Up @@ -228,10 +227,7 @@ def viewcode_find(app, modname):
children = getattr(obj, "children", ())
stack.extend((full_name + ".", gchild) for gchild in children)

if module.obj["encoding"]:
stream = io.open(module.obj["file_path"], encoding=module.obj["encoding"])
else:
stream = open(module.obj["file_path"], encoding="utf-8")
stream = open(module.obj["file_path"], encoding=module.obj.get("encoding", "utf-8"))

with stream as in_f:
source = in_f.read()
Expand Down
1 change: 1 addition & 0 deletions docs/changes/449.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop Python <=3.7 syntax
4 changes: 2 additions & 2 deletions tests/python/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def build(test_dir, **kwargs):
suppress.append("app.add_directive")
suppress.append("app.add_role")

os.chdir("tests/python/{0}".format(test_dir))
os.chdir(f"tests/python/{test_dir}")
rebuild(**kwargs)

yield build
Expand All @@ -60,7 +60,7 @@ def parse():

def parser(path):
if path not in cache:
with io.open(path, encoding="utf8") as file_handle:
with open(path, encoding="utf8") as file_handle:
cache[path] = BeautifulSoup(file_handle, features="html.parser")

return cache[path]
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pep695/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/py310unionpipe/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/py38positionalparams/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 1 addition & 1 deletion tests/python/py38positionalparams/example/example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""

from typing import Optional


Expand Down
2 changes: 0 additions & 2 deletions tests/python/py3example/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 1 addition & 1 deletion tests/python/py3example/example/example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""

import asyncio
import typing
from typing import ClassVar, Dict, Iterable, Generic, List, TypeVar, Union, overload
Expand Down
2 changes: 0 additions & 2 deletions tests/python/py3implicitnamespace/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pyannotationcommentsexample/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""

from typing import ClassVar, Dict, Iterable, List, Union

max_rating = 10 # type: int
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pyautodoc_typehints/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pyemptyexample/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pyexample/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
6 changes: 3 additions & 3 deletions tests/python/pyexample/example/example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""

from functools import cached_property

A_TUPLE = ("a", "b")
Expand All @@ -11,7 +11,7 @@
"""A list to be rendered as a list."""


class Foo(object):
class Foo:
"""Can we parse arguments from the class docstring?
:param attr: Set an attribute.
Expand All @@ -23,7 +23,7 @@ class Foo(object):
another_class_var = 42
"""Another class var docstring"""

class Meta(object):
class Meta:
"""A nested class just to test things out"""

@classmethod
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pyiexample/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 1 addition & 1 deletion tests/python/pyiexample/example/example.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class DoNotFindThis(object):
class DoNotFindThis:
"""pyi files should be preferred."""
2 changes: 0 additions & 2 deletions tests/python/pyiexample2/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
3 changes: 1 addition & 2 deletions tests/python/pyiexample2/example/example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""


class Foo(object):
class Foo:
pass
2 changes: 0 additions & 2 deletions tests/python/pyisubmoduleinit/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 1 addition & 1 deletion tests/python/pypackagecomplex/complex/_private_module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PrivateClass(object):
class PrivateClass:
"""A private class with public facing methods."""

def public_method():
Expand Down
1 change: 0 additions & 1 deletion tests/python/pypackagecomplex/complex/binary_data.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
data = b"\xf0\xf0\xf0"
4 changes: 2 additions & 2 deletions tests/python/pypackagecomplex/complex/foo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
__all__ = ["PublicClass", "Foo"]


class Foo(object):
class Foo:
class_var = 42 #: Class var docstring

another_class_var = 42
"""Another class var docstring"""

class Meta(object):
class Meta:
"""A nested class just to test things out"""

@classmethod
Expand Down
1 change: 0 additions & 1 deletion tests/python/pypackagecomplex/complex/unicode_data.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
unicode_str = "αβγδεζηθικλμνξοπρςστυφχψ"
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
]


class SimpleClass(object):
class SimpleClass:
def simple_method(self):
return 5


class NotAllClass(object):
class NotAllClass:
def not_all_method(self):
return 5

Expand Down
2 changes: 0 additions & 2 deletions tests/python/pypackagecomplex/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
2 changes: 0 additions & 2 deletions tests/python/pypackageexample/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
Loading

0 comments on commit 83560ba

Please sign in to comment.