diff --git a/penman/__init__.py b/penman/__init__.py index 6daeb54..8b89df5 100644 --- a/penman/__init__.py +++ b/penman/__init__.py @@ -35,35 +35,35 @@ __version__, __version_info__, ) -from penman.exceptions import ( - PenmanError, - DecodeError, -) -from penman.tree import Tree -from penman.graph import ( - Triple, - Graph, -) -from penman.layout import ( - interpret, - configure, +from penman._format import ( + format, + format_triples, ) from penman._parse import ( - parse, iterparse, + parse, parse_triples, ) -from penman._format import ( - format, - format_triples, -) from penman.codec import ( PENMANCodec, _decode as decode, - _iterdecode as iterdecode, + _dump as dump, + _dumps as dumps, _encode as encode, + _iterdecode as iterdecode, _load as load, _loads as loads, - _dump as dump, - _dumps as dumps, ) +from penman.exceptions import ( + DecodeError, + PenmanError, +) +from penman.graph import ( + Graph, + Triple, +) +from penman.layout import ( + configure, + interpret, +) +from penman.tree import Tree diff --git a/penman/__main__.py b/penman/__main__.py index fd79560..f30ba0f 100644 --- a/penman/__main__.py +++ b/penman/__main__.py @@ -1,16 +1,14 @@ # -*- coding: utf-8 -*- -import sys -import os import argparse import json import logging +import os +import sys +from penman import layout, transform from penman.__about__ import __version__ -from penman.model import Model -from penman import layout from penman.codec import PENMANCodec -from penman import transform - +from penman.model import Model # Names of functions allowed for ordering triples/branches; we cannot # resolve them to the actual functions until the model is loaded. If @@ -108,13 +106,13 @@ def _check(g, model): i = 1 errors = model.errors(g) if errors: - for triple, errors in errors.items(): + for triple, error_messages in errors.items(): if triple: context = '({}) '.format(' '.join(map(str, triple))) else: context = '' - for error in errors: - g.metadata[f'error-{i}'] = context + error + for error_message in error_messages: + g.metadata[f'error-{i}'] = context + error_message i += 1 return 1 else: diff --git a/penman/_format.py b/penman/_format.py index 90ff838..832f774 100644 --- a/penman/_format.py +++ b/penman/_format.py @@ -1,8 +1,8 @@ -from typing import Optional, Union, List, Iterable +from typing import Iterable, List, Optional, Union +from penman.tree import Tree, is_atomic from penman.types import BasicTriple -from penman.tree import (Tree, is_atomic) def format(tree: Tree, diff --git a/penman/_lexer.py b/penman/_lexer.py index c1659ab..e1c0de3 100644 --- a/penman/_lexer.py +++ b/penman/_lexer.py @@ -4,13 +4,12 @@ Classes and functions for lexing PENMAN strings. """ -from typing import Union, Iterable, Iterator, NamedTuple, Pattern, Optional -import re import logging +import re +from typing import Iterable, Iterator, NamedTuple, Optional, Pattern, Union from penman.exceptions import DecodeError - logger = logging.getLogger(__name__) @@ -126,7 +125,7 @@ def expect(self, *choices): try: token = self.next() except StopIteration: - raise self.error('Unexpected end of input') + raise self.error('Unexpected end of input') from None if token.type not in choices: raise self.error('Expected: {}'.format(', '.join(choices)), token=token) diff --git a/penman/_parse.py b/penman/_parse.py index c9ade24..52b3b8b 100644 --- a/penman/_parse.py +++ b/penman/_parse.py @@ -1,19 +1,18 @@ -from typing import Union, Iterable, Iterator, List import logging +from typing import Iterable, Iterator, List, Union -from penman.types import ( - Target, - BasicTriple, -) -from penman.tree import Tree from penman._lexer import ( PENMAN_RE, TRIPLE_RE, - lex, TokenIterator, + lex, +) +from penman.tree import Tree +from penman.types import ( + BasicTriple, + Target, ) - logger = logging.getLogger('penman') diff --git a/penman/codec.py b/penman/codec.py index 9f20ee7..eb508e8 100644 --- a/penman/codec.py +++ b/penman/codec.py @@ -3,27 +3,26 @@ """ Serialization of PENMAN graphs. """ -from typing import Optional, Union, Iterable, Iterator, List, IO from pathlib import Path +from typing import IO, Iterable, Iterator, List, Optional, Union -from penman.types import ( - Variable, - BasicTriple, +from penman import layout +from penman._format import ( + format, + format_triples, ) -from penman.tree import Tree -from penman.graph import Graph -from penman.model import Model from penman._parse import ( - parse, iterparse, + parse, parse_triples, ) -from penman._format import ( - format, - format_triples, +from penman.graph import Graph +from penman.model import Model +from penman.tree import Tree +from penman.types import ( + BasicTriple, + Variable, ) -from penman import layout - # "Utility" types; not Penman-specific diff --git a/penman/constant.py b/penman/constant.py index 9119b25..f4141b2 100644 --- a/penman/constant.py +++ b/penman/constant.py @@ -3,13 +3,12 @@ Functions for working with constant values. """ -from typing import Union -from enum import Enum import json +from enum import Enum +from typing import Union -from penman.types import Constant from penman.exceptions import ConstantError - +from penman.types import Constant pytype = type # store because type() is redefined below diff --git a/penman/graph.py b/penman/graph.py index fe4ea1d..c8b18cf 100644 --- a/penman/graph.py +++ b/penman/graph.py @@ -4,21 +4,20 @@ Data structures for Penman graphs and triples. """ -from typing import (Union, Optional, Mapping, List, Dict, Set, NamedTuple) -from collections import defaultdict import copy +from collections import defaultdict +from typing import Dict, List, Mapping, NamedTuple, Optional, Set, Union +from penman.epigraph import Epidata from penman.exceptions import GraphError from penman.types import ( - Variable, + BasicTriple, Constant, Role, Target, - BasicTriple, Triples, + Variable, ) -from penman.epigraph import Epidata - CONCEPT_ROLE = ':instance' diff --git a/penman/interface.py b/penman/interface.py index 338337c..84b07b7 100644 --- a/penman/interface.py +++ b/penman/interface.py @@ -10,15 +10,17 @@ from penman.codec import ( # noqa: F401 _decode as decode, - _iterdecode as iterdecode, + _dump as dump, + _dumps as dumps, _encode as encode, + _iterdecode as iterdecode, _load as load, _loads as loads, - _dump as dump, - _dumps as dumps, ) warnings.warn( 'The penman.interface module is deprecated. Use the functions from ' 'the penman module directly, e.g., penman.decode().', - DeprecationWarning) + DeprecationWarning, + stacklevel=2, +) diff --git a/penman/layout.py b/penman/layout.py index bd98296..58f4d32 100644 --- a/penman/layout.py +++ b/penman/layout.py @@ -50,18 +50,17 @@ ('b', ':ARG0', 'd')] : POP """ -from typing import Union, Mapping, Callable, Any, List, Set, cast, Optional import copy import logging +from typing import Any, Callable, List, Mapping, Optional, Set, Union, cast -from penman.exceptions import LayoutError -from penman.types import (Variable, Role, BasicTriple, Branch, Node) from penman.epigraph import Epidatum -from penman.surface import (Alignment, RoleAlignment) -from penman.tree import (Tree, is_atomic) -from penman.graph import (Graph, CONCEPT_ROLE) +from penman.exceptions import LayoutError +from penman.graph import CONCEPT_ROLE, Graph from penman.model import Model - +from penman.surface import Alignment, RoleAlignment +from penman.tree import Tree, is_atomic +from penman.types import BasicTriple, Branch, Node, Role, Variable logger = logging.getLogger(__name__) diff --git a/penman/model.py b/penman/model.py index b1afdfa..e9750b9 100644 --- a/penman/model.py +++ b/penman/model.py @@ -4,22 +4,14 @@ Semantic models for interpreting graphs. """ -from typing import ( - cast, Optional, Tuple, List, Dict, Set, Iterable, Mapping, Any) +import random import re from collections import defaultdict -import random +from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, cast from penman.exceptions import ModelError -from penman.types import ( - Variable, - Role, - Constant, - Target, - BasicTriple -) from penman.graph import CONCEPT_ROLE, Graph - +from penman.types import BasicTriple, Constant, Role, Target, Variable _ReificationSpec = Tuple[Role, Constant, Role, Role] _Reified = Tuple[Constant, Role, Role] diff --git a/penman/models/amr.py b/penman/models/amr.py index 35b83c9..51b7006 100644 --- a/penman/models/amr.py +++ b/penman/models/amr.py @@ -5,7 +5,6 @@ from penman.model import Model - #: The roles are the edge labels of reifications. The purpose of roles #: in a :class:`~penman.model.Model` is mainly to define the set of #: valid roles, but they map to arbitrary data which is not used by diff --git a/penman/models/noop.py b/penman/models/noop.py index ec14a93..df708ee 100644 --- a/penman/models/noop.py +++ b/penman/models/noop.py @@ -2,8 +2,8 @@ No-op semantic model definition. """ -from penman.types import BasicTriple from penman.model import Model +from penman.types import BasicTriple class NoOpModel(Model): diff --git a/penman/surface.py b/penman/surface.py index 2174cb1..c9394a3 100644 --- a/penman/surface.py +++ b/penman/surface.py @@ -4,13 +4,12 @@ Surface strings, tokens, and alignments. """ -from typing import TypeVar, Type, Mapping, Tuple, Optional +from typing import Mapping, Optional, Tuple, Type, TypeVar -from penman.types import BasicTriple -from penman.graph import Graph from penman.epigraph import Epidatum from penman.exceptions import SurfaceError - +from penman.graph import Graph +from penman.types import BasicTriple T = TypeVar('T', bound='AlignmentMarker') # for classmethods diff --git a/penman/transform.py b/penman/transform.py index dc7da33..be91ee7 100644 --- a/penman/transform.py +++ b/penman/transform.py @@ -3,24 +3,23 @@ Tree and graph transformations. """ -from typing import Optional, Dict, Set, List, Tuple import logging +from typing import Dict, List, Optional, Set, Tuple -from penman.types import (Variable, Target, BasicTriple, Node) +from penman.epigraph import Epidata, Epidatum from penman.exceptions import ModelError -from penman.epigraph import (Epidatum, Epidata) -from penman.surface import (Alignment, RoleAlignment, alignments) -from penman.tree import (Tree, is_atomic) -from penman.graph import (Graph, CONCEPT_ROLE) -from penman.model import Model +from penman.graph import CONCEPT_ROLE, Graph from penman.layout import ( - Push, - Pop, POP, + Pop, + Push, appears_inverted, get_pushed_variable, ) - +from penman.model import Model +from penman.surface import Alignment, RoleAlignment, alignments +from penman.tree import Tree, is_atomic +from penman.types import BasicTriple, Node, Target, Variable logger = logging.getLogger(__name__) @@ -60,7 +59,7 @@ def canonicalize_roles(t: Tree, model: Model) -> Tree: def _canonicalize_node(node: Node, model: Model) -> Node: var, edges = node canonical_edges = [] - for i, edge in enumerate(edges): + for edge in edges: role, tgt = edge # alignments aren't parsed off yet, so handle them superficially role, tilde, alignment = role.partition('~') diff --git a/penman/tree.py b/penman/tree.py index 3dfa52b..af40ed5 100644 --- a/penman/tree.py +++ b/penman/tree.py @@ -3,10 +3,9 @@ Definitions of tree structures. """ -from typing import Dict, List, Tuple, Set, Mapping, Any, Iterator, Optional - -from penman.types import (Variable, Branch, Node) +from typing import Any, Dict, Iterator, List, Mapping, Optional, Set, Tuple +from penman.types import Branch, Node, Variable _Step = Tuple[Tuple[int, ...], Branch] # see Tree.walk() diff --git a/penman/types.py b/penman/types.py index bb913b4..eec421d 100644 --- a/penman/types.py +++ b/penman/types.py @@ -3,8 +3,7 @@ Basic types used by various Penman modules. """ -from typing import (Union, Iterable, Tuple, List, Any) - +from typing import Any, Iterable, List, Tuple, Union Variable = str Constant = Union[str, float, int, None] # None for missing values diff --git a/pyproject.toml b/pyproject.toml index e3621fa..3c673d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -81,3 +81,7 @@ select = [ "F", # Pyflakes "W", # pycodestyle warnings ] + +[tool.ruff.lint.isort] +combine-as-imports = true +force-wrap-aliases = true