Skip to content

Commit

Permalink
style: add from __future__ import annotations (#2576)
Browse files Browse the repository at this point in the history
Add `from __future__ import annotations` to files with type hints. Using
this simplifies type hints as they no longer require quoting.

This is in preparation for enabling Ruff rule UP037 (quoted-annotation,
Remove quotes from type annotation)
[[ref](https://beta.ruff.rs/docs/rules/#pyupgrade-up)].
  • Loading branch information
aucampia authored Sep 5, 2023
1 parent b585861 commit 16047eb
Show file tree
Hide file tree
Showing 81 changed files with 148 additions and 7 deletions.
1 change: 1 addition & 0 deletions devtools/diffrtpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Then attach ``/var/tmp/compact.diff`` to the PR.
"""

from __future__ import annotations

import argparse
import logging
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
# https://www.sphinx-doc.org/en/master/usage/configuration.html
from __future__ import annotations

import logging
import os
Expand Down
1 change: 1 addition & 0 deletions examples/secure_with_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
The code in the example then verifies that the audit hook is blocking access to
URLs and files as expected.
"""
from __future__ import annotations

import logging
import os
Expand Down
2 changes: 2 additions & 0 deletions examples/secure_with_urlopen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
This example demonstrates how to use a custom global URL opener installed with `urllib.request.install_opener` to block access to URLs.
"""
from __future__ import annotations

import http.client
import logging
import os
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ select = [
"I", # isort
"N", # pep8-naming
"RUF100", # Unused noqa directive
"UP010", # Unnecessary __future__ import for target Python version
"FA", # flake8-future-annotations
]

ignore = [
Expand Down
2 changes: 1 addition & 1 deletion rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
_:cb558f30e21ddfc05ca53108348338ade8
<http://example.org/ns#label> "B" .
"""
from __future__ import absolute_import, division, print_function
from __future__ import annotations

# TODO:
# - Doesn't handle quads.
Expand Down
1 change: 1 addition & 0 deletions rdflib/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
TODO:
"""
from __future__ import annotations

__all__ = [
"Error",
Expand Down
1 change: 1 addition & 0 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
>>> RDFS.seeAlso
rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#seeAlso')
"""
from __future__ import annotations

import logging
import warnings
Expand Down
3 changes: 2 additions & 1 deletion rdflib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
.. __: http://peak.telecommunity.com/DevCenter/setuptools#dynamic-discovery-of-services-and-plugins
"""
from __future__ import annotations

from importlib.metadata import EntryPoint, entry_points
from typing import (
Expand Down Expand Up @@ -102,7 +103,7 @@ def getClass(self) -> Type[PluginT]: # noqa: N802


class PKGPlugin(Plugin[PluginT]):
def __init__(self, name: str, kind: Type[PluginT], ep: "EntryPoint"):
def __init__(self, name: str, kind: Type[PluginT], ep: EntryPoint):
self.name = name
self.kind = kind
self.ep = ep
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/hext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
HextuplesSerializer RDF graph serializer for RDFLib.
See <https://github.com/ontola/hextuples> for details about the format.
"""
from __future__ import annotations

import json
import warnings
from typing import IO, Optional, Type, Union
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
# but we should consider streaming the output to deal with arbitrarily large
# graphs.

from __future__ import annotations

import warnings
from typing import IO, Optional

Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/nquads.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import warnings
from typing import IO, Optional

Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/rdfxml.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import xml.dom.minidom
from typing import IO, Dict, Optional, Set
from xml.sax.saxutils import escape, quoteattr
Expand Down
1 change: 1 addition & 0 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Trig RDF graph serializer for RDFLib.
See <http://www.w3.org/TR/trig/> for syntax specification.
"""
from __future__ import annotations

from typing import IO, TYPE_CHECKING, Dict, List, Optional, Tuple, Union

Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/trix.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import IO, Optional

from rdflib.graph import ConjunctiveGraph, Graph
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/sparql/results/rdfresults.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import IO, Any, MutableMapping, Optional, Union

from rdflib.graph import Graph
Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/sparql/results/tsvresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
It is implemented with pyparsing, reusing the elements from the SPARQL Parser
"""
from __future__ import annotations

import codecs # noqa: I001
import codecs
import typing
from typing import IO, Union

Expand All @@ -30,9 +31,8 @@
)
from rdflib.plugins.sparql.parserutils import Comp, CompValue, Param
from rdflib.query import Result, ResultParser
from rdflib.term import BNode
from rdflib.term import BNode, URIRef
from rdflib.term import Literal as RDFLiteral
from rdflib.term import URIRef

ParserElement.setDefaultWhitespaceChars(" \n")

Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/sparql/results/txtresults.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import IO, List, Optional, Union

from rdflib.namespace import NamespaceManager
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/sparql/results/xmlresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Authors: Drew Perttula, Gunnar Aastrand Grimnes
"""
from __future__ import annotations

import logging
import xml.etree.ElementTree as xml_etree # noqa: N813
from io import BytesIO
Expand Down
1 change: 1 addition & 0 deletions rdflib/plugins/stores/auditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system fails): A and I out of ACID.
"""
from __future__ import annotations

import threading
from typing import TYPE_CHECKING, Any, Generator, Iterator, List, Optional, Tuple
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/stores/berkeleydb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
from os import mkdir
from os.path import abspath, exists
Expand Down
4 changes: 3 additions & 1 deletion rdflib/plugins/stores/sparqlconnector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import base64
import copy
import logging
Expand Down Expand Up @@ -40,7 +42,7 @@ def __init__(
query_endpoint: Optional[str] = None,
update_endpoint: Optional[str] = None,
returnFormat: str = "xml", # noqa: N803
method: "te.Literal['GET', 'POST', 'POST_FORM']" = "GET",
method: te.Literal["GET", "POST", "POST_FORM"] = "GET",
auth: Optional[Tuple[str, str]] = None,
**kwargs,
):
Expand Down
2 changes: 2 additions & 0 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This was first done in layer-cake, and then ported to RDFLib
"""
from __future__ import annotations

import collections
import re
from typing import (
Expand Down
2 changes: 2 additions & 0 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Numerical Ranges
"""
from __future__ import annotations

import abc
import re
from fractions import Fraction
Expand Down
1 change: 1 addition & 0 deletions rdflib/tools/chunk_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
There is an option to preserve any prefixes declared for the original graph in the first
file, which will be a Turtle file.
"""
from __future__ import annotations

from contextlib import ExitStack, contextmanager
from pathlib import Path
Expand Down
2 changes: 2 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import sys
from contextlib import ExitStack

Expand Down
2 changes: 2 additions & 0 deletions test/data/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python
from __future__ import annotations

import argparse
import enum
import logging
Expand Down
2 changes: 2 additions & 0 deletions test/data/suites/trix/test_trix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""This runs the TriX tests for RDFLib's informally-assembled TriX
test suite."""
from __future__ import annotations

from test.data import TEST_DATA_DIR
from test.utils.manifest import RDFTest, read_manifest
from test.utils.namespace import RDFT
Expand Down
2 changes: 2 additions & 0 deletions test/jsonld/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import List

from rdflib import parser, plugin, serializer
Expand Down
1 change: 1 addition & 0 deletions test/jsonld/test_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
JSON-LD Context Spec
"""
from __future__ import annotations

import json
from functools import wraps
Expand Down
2 changes: 2 additions & 0 deletions test/jsonld/test_nested_arrays.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from test.utils.namespace import EGDC

from rdflib import Graph, Literal
Expand Down
2 changes: 2 additions & 0 deletions test/jsonld/test_onedotone.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import os
from os import chdir, environ, getcwd
Expand Down
2 changes: 2 additions & 0 deletions test/jsonld/test_testsuite.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from os import chdir, environ, getcwd
from os import path as p
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/parser/example/rdflib/plugin/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Set, Tuple

from rdflib.namespace import Namespace
Expand Down
2 changes: 2 additions & 0 deletions test/test_dataset/test_dataset_default_graph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import itertools
import logging
from test.data import TEST_DATA_DIR
Expand Down
2 changes: 2 additions & 0 deletions test/test_graph/test_diff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dataclasses import dataclass, field
from test.utils import (
COLLAPSED_BNODE,
Expand Down
2 changes: 2 additions & 0 deletions test/test_graph/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import os
from pathlib import Path
Expand Down
2 changes: 2 additions & 0 deletions test/test_graph/test_graph_http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import re
from http.server import BaseHTTPRequestHandler
Expand Down
2 changes: 2 additions & 0 deletions test/test_graph/test_graph_redirect.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from test.data import SIMPLE_TRIPLE_GRAPH, TEST_DATA_DIR
from test.utils import GraphHelper
from test.utils.http import MethodName, MockHTTPResponse
Expand Down
1 change: 1 addition & 0 deletions test/test_graph/test_graph_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for usage of the Store interface from Graph/NamespaceManager.
"""
from __future__ import annotations

import itertools
import logging
Expand Down
2 changes: 2 additions & 0 deletions test/test_graph/test_skolemization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import re
from test.utils import GraphHelper
Expand Down
2 changes: 2 additions & 0 deletions test/test_misc/test_networking_redirect.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import ExitStack
from copy import deepcopy
from test.utils.http import headers_as_message as headers_as_message
Expand Down
2 changes: 2 additions & 0 deletions test/test_misc/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import importlib
import logging
import shutil
Expand Down
2 changes: 2 additions & 0 deletions test/test_misc/test_security.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import enum
import http.client
import itertools
Expand Down
2 changes: 2 additions & 0 deletions test/test_namespace/test_definednamespace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import inspect
import logging
import subprocess
Expand Down
2 changes: 2 additions & 0 deletions test/test_namespace/test_namespace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from test.utils.outcome import OutcomeChecker, OutcomePrimitive
from typing import Any, Optional
from warnings import warn
Expand Down
1 change: 1 addition & 0 deletions test/test_parsers/test_parser_turtlelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module contains tests for the parsing of the turtle family of formats: N3,
Turtle, NTriples, NQauds and TriG.
"""
from __future__ import annotations

import enum
import itertools
Expand Down
2 changes: 2 additions & 0 deletions test/test_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
from typing import Union

Expand Down
2 changes: 2 additions & 0 deletions test/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
& test_serializer_hext.py)
"""
from __future__ import annotations

import enum
import logging
import os.path
Expand Down
2 changes: 2 additions & 0 deletions test/test_serializers/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import enum
import itertools
import logging
Expand Down
2 changes: 2 additions & 0 deletions test/test_serializers/test_serializer_jsonld.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import logging
import pprint
Expand Down
Loading

0 comments on commit 16047eb

Please sign in to comment.