Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PyUpgrade and import cleaning to Ruff #28

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydough/database_connectors/database_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Any


class DatabaseConnection(object):
class DatabaseConnection:
"""
Class that manages a generic DB API 2.0 connection. This basically
dispatches to the DB API 2.0 API on the underlying object and represents
Expand Down
12 changes: 6 additions & 6 deletions pydough/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"SimpleTableMetadata",
]

from .parse import parse_json_metadata_from_file
from .graphs import GraphMetadata
from .collections import CollectionMetadata, SimpleTableMetadata
from .errors import PyDoughMetadataException
from .graphs import GraphMetadata
from .parse import parse_json_metadata_from_file
from .properties import (
PropertyMetadata,
TableColumnMetadata,
SimpleJoinMetadata,
CartesianProductMetadata,
CompoundRelationshipMetadata,
PropertyMetadata,
SimpleJoinMetadata,
TableColumnMetadata,
)
from .errors import PyDoughMetadataException
3 changes: 1 addition & 2 deletions pydough/metadata/abstract_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
__all__ = ["AbstractMetadata"]

from abc import ABC, abstractmethod

from typing import MutableMapping, MutableSequence
from collections.abc import MutableMapping, MutableSequence


class AbstractMetadata(ABC):
Expand Down
24 changes: 12 additions & 12 deletions pydough/metadata/collections/collection_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"""

from abc import abstractmethod
from collections import defaultdict
from collections.abc import MutableMapping, MutableSequence

from typing import MutableSequence, MutableMapping, Set, Type
from pydough.metadata.abstract_metadata import AbstractMetadata
from pydough.metadata.errors import (
HasPropertyWith,
HasType,
PyDoughMetadataException,
is_valid_name,
is_string,
HasType,
HasPropertyWith,
is_valid_name,
)
from collections import defaultdict
from pydough.metadata.abstract_metadata import AbstractMetadata
from pydough.metadata.graphs import GraphMetadata


Expand All @@ -32,12 +32,12 @@ class CollectionMetadata(AbstractMetadata):

# Set of names of of fields that can be included in the JSON
# object describing a collection. Implementations should extend this.
allowed_fields: Set[str] = {"type", "properties"}
allowed_fields: set[str] = {"type", "properties"}

def __init__(self, name: str, graph: GraphMetadata):
from pydough.metadata.properties import (
PropertyMetadata,
InheritedPropertyMetadata,
PropertyMetadata,
)

is_valid_name.verify(name, "name")
Expand Down Expand Up @@ -194,8 +194,8 @@ def verify_allows_property(
to insert into the collection.
"""
from pydough.metadata.properties import (
PropertyMetadata,
InheritedPropertyMetadata,
PropertyMetadata,
)

# First, make sure that the candidate property is indeed a property
Expand Down Expand Up @@ -323,7 +323,7 @@ def get_property(self, property_name: str) -> AbstractMetadata:
@staticmethod
def get_class_for_collection_type(
name: str, error_name: str
) -> Type["CollectionMetadata"]:
) -> type["CollectionMetadata"]:
"""
Fetches the PropertyType implementation class for a string
representation of the collection type.
Expand All @@ -340,7 +340,7 @@ def get_class_for_collection_type(
`PyDoughMetadataException` if the string does not correspond
to a known class type.
"""
from . import SimpleTableMetadata
from .simple_table_metadata import SimpleTableMetadata

match name:
case "simple_table":
Expand Down Expand Up @@ -416,7 +416,7 @@ def parse_from_json(

# Dispatch to a specific parsing procedure based on the type of
# collection.
property_class: Type[CollectionMetadata] = (
property_class: type[CollectionMetadata] = (
CollectionMetadata.get_class_for_collection_type(
collection_json["type"], error_name
)
Expand Down
38 changes: 20 additions & 18 deletions pydough/metadata/collections/simple_table_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@
TODO: add file-level docstring
"""

from typing import MutableSequence, Union, Set, Tuple
from collections.abc import MutableSequence

from pydough.metadata.abstract_metadata import AbstractMetadata
from pydough.metadata.errors import (
HasPropertyWith,
unique_properties_predicate,
NoExtraKeys,
is_string,
PyDoughMetadataException,
is_string,
unique_properties_predicate,
)
from pydough.metadata.abstract_metadata import AbstractMetadata
from pydough.metadata.graphs import GraphMetadata
from . import CollectionMetadata
from pydough.metadata.properties import (
PropertyMetadata,
TableColumnMetadata,
SimpleJoinMetadata,
CompoundRelationshipMetadata,
CartesianProductMetadata,
CompoundRelationshipMetadata,
InheritedPropertyMetadata,
PropertyMetadata,
SimpleJoinMetadata,
TableColumnMetadata,
)

from .collection_metadata import CollectionMetadata


class SimpleTableMetadata(CollectionMetadata):
"""
Expand All @@ -32,7 +34,7 @@ class SimpleTableMetadata(CollectionMetadata):

# Set of names of of fields that can be included in the JSON
# object describing a simple table collection.
allowed_fields: Set[str] = CollectionMetadata.allowed_fields | {
allowed_fields: set[str] = CollectionMetadata.allowed_fields | {
"table_path",
"unique_properties",
}
Expand All @@ -42,15 +44,15 @@ def __init__(
name: str,
graph,
table_path: str,
unique_properties: MutableSequence[Union[str, MutableSequence[str]]],
unique_properties: MutableSequence[str | MutableSequence[str]],
):
super().__init__(name, graph)
is_string.verify(table_path, f"Property 'table_path' of {self.error_name}")
unique_properties_predicate.verify(
unique_properties, f"property 'unique_properties' of {self.error_name}"
)
self._table_path: str = table_path
self._unique_properties: MutableSequence[Union[str, MutableSequence[str]]] = (
self._unique_properties: MutableSequence[str | MutableSequence[str]] = (
unique_properties
)

Expand All @@ -63,7 +65,7 @@ def table_path(self) -> str:
return self._table_path

@property
def unique_properties(self) -> MutableSequence[Union[str, MutableSequence[str]]]:
def unique_properties(self) -> MutableSequence[str | MutableSequence[str]]:
"""
The list of all names of properties of the collection that are
guaranteed to be unique within the collection. Entries that are a
Expand Down Expand Up @@ -91,17 +93,17 @@ def verify_complete(self) -> None:
# Extract all names properties used in the uniqueness of the table
# collection, ensuring there are no invalid duplicates.
malformed_unique_msg: str = f"{self.error_name} has malformed unique properties set: {self.unique_properties}"
unique_property_combinations: Set[Tuple] = set()
unique_property_names: Set[str] = set()
unique_property_combinations: set[tuple] = set()
unique_property_names: set[str] = set()
for unique_property in self.unique_properties:
unique_property_set: Set[str]
unique_property_set: set[str]
if isinstance(unique_property, str):
unique_property_set = {unique_property}
else:
unique_property_set = set(unique_property)
if len(unique_property_set) < len(unique_property):
raise PyDoughMetadataException(malformed_unique_msg)
unique_property_tuple: Tuple = tuple(sorted(unique_property_set))
unique_property_tuple: tuple = tuple(sorted(unique_property_set))
if unique_property_tuple in unique_property_combinations:
raise PyDoughMetadataException(malformed_unique_msg)
unique_property_combinations.add(unique_property_tuple)
Expand Down Expand Up @@ -219,7 +221,7 @@ def parse_from_json(
# Extract the relevant properties from the JSON to build the new
# collection, then add it to the graph.
table_path: str = collection_json["table_path"]
unique_properties: MutableSequence[Union[str, MutableSequence[str]]] = (
unique_properties: MutableSequence[str | MutableSequence[str]] = (
collection_json["unique_properties"]
)
new_collection: SimpleTableMetadata = SimpleTableMetadata(
Expand Down
8 changes: 4 additions & 4 deletions pydough/metadata/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
]


from typing import MutableSequence, Set, Optional
from abc import ABC, abstractmethod
from collections.abc import MutableSequence


class PyDoughMetadataException(Exception):
Expand Down Expand Up @@ -105,8 +105,8 @@ class NoExtraKeys(PyDoughPredicate):
besides those that have been specified.
"""

def __init__(self, valid_keys: Set[str]):
self.valid_keys: Set[str] = valid_keys
def __init__(self, valid_keys: set[str]):
self.valid_keys: set[str] = valid_keys

def accept(self, obj: object) -> bool:
return isinstance(obj, dict) and set(obj) <= self.valid_keys
Expand Down Expand Up @@ -135,7 +135,7 @@ def error_message(self, error_name: str) -> str:
class HasType(PyDoughPredicate):
"""Predicate class to check that an object has a certain type"""

def __init__(self, desired_type: type, type_name: Optional[str] = None):
def __init__(self, desired_type: type, type_name: str | None = None):
self.desired_type: type = desired_type
self.type_name: str = (
self.desired_type.__name__ if type_name is None else type_name
Expand Down
4 changes: 2 additions & 2 deletions pydough/metadata/graphs/graph_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
TODO: add file-level docstring
"""

from typing import MutableSequence, MutableMapping
from collections import defaultdict
from pydough.metadata.errors import PyDoughMetadataException, is_valid_name, HasType
from collections.abc import MutableMapping, MutableSequence

from pydough.metadata.abstract_metadata import AbstractMetadata
from pydough.metadata.errors import HasType, PyDoughMetadataException, is_valid_name


class GraphMetadata(AbstractMetadata):
Expand Down
24 changes: 12 additions & 12 deletions pydough/metadata/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

__all__ = ["parse_json_metadata_from_file"]

from typing import MutableMapping, MutableSequence, Tuple, Set, MutableSet
from .graphs import GraphMetadata
import json
from collections import deque
from collections.abc import MutableMapping, MutableSequence, MutableSet
from dataclasses import dataclass

from .collections import CollectionMetadata
from .errors import (
PyDoughMetadataException,
HasPropertyWith,
HasType,
PyDoughMetadataException,
)
from collections import deque
from .collections import CollectionMetadata
from .graphs import GraphMetadata
from .properties import PropertyMetadata
import json

from dataclasses import dataclass


# The way a property is stored until it is parsed.
Expand Down Expand Up @@ -53,7 +53,7 @@ def parse_json_metadata_from_file(file_path: str, graph_name: str) -> GraphMetad
`PyDoughMetadataException`: if the file is malformed in any way that
prevents parsing it to obtain the desired graph.
"""
with open(file_path, "r") as f:
with open(file_path) as f:
as_json = json.load(f)
if not isinstance(as_json, dict):
raise PyDoughMetadataException(
Expand Down Expand Up @@ -174,7 +174,7 @@ def topologically_sort_properties(
# identifying `(collection_name, property_name)` tuple (hereafter
# referred to as the `property`) and the values are a tuple of the
# property's JSON and its index in the original raw_properties list.
reformatted_properties: MutableMapping[PropertyKey, Tuple[dict, int]] = {
reformatted_properties: MutableMapping[PropertyKey, tuple[dict, int]] = {
PropertyKey(property.collection_name, property.property_name): (
property.property_json,
i,
Expand Down Expand Up @@ -204,7 +204,7 @@ def topologically_sort_properties(


def get_property_dependencies(
reformatted_properties: MutableMapping[PropertyKey, Tuple[dict, int]],
reformatted_properties: MutableMapping[PropertyKey, tuple[dict, int]],
) -> MutableSequence[MutableSet[int]]:
"""
Infers the set of dependencies for each property.
Expand Down Expand Up @@ -249,7 +249,7 @@ def get_property_dependencies(

# A dictionary mapping each property to the set of all inherited property
# names that are associated with it.
compound_inherited_aliases: MutableMapping[PropertyKey, Set[str]] = {}
compound_inherited_aliases: MutableMapping[PropertyKey, set[str]] = {}

def get_true_property(property: PropertyKey) -> PropertyKey | None:
"""
Expand Down
10 changes: 5 additions & 5 deletions pydough/metadata/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"ScalarAttributeMetadata",
]

from .property_metadata import PropertyMetadata
from .reversible_property_metadata import ReversiblePropertyMetadata
from .table_column_metadata import TableColumnMetadata
from .simple_join_metadata import SimpleJoinMetadata
from .cartesian_product_metadata import CartesianProductMetadata
from .compound_relationship_metadata import CompoundRelationshipMetadata
from .inherited_property_metadata import InheritedPropertyMetadata
from .subcollection_relationship_metadata import SubcollectionRelationshipMetadata
from .property_metadata import PropertyMetadata
from .reversible_property_metadata import ReversiblePropertyMetadata
from .scalar_attribute_metadata import ScalarAttributeMetadata
from .simple_join_metadata import SimpleJoinMetadata
from .subcollection_relationship_metadata import SubcollectionRelationshipMetadata
from .table_column_metadata import TableColumnMetadata
10 changes: 6 additions & 4 deletions pydough/metadata/properties/cartesian_product_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

__all__ = ["CartesianProductMetadata"]

from pydough.metadata.errors import HasPropertyWith, HasType, is_string, NoExtraKeys
from typing import Set

from pydough.metadata.collections import CollectionMetadata
from . import ReversiblePropertyMetadata, PropertyMetadata
from pydough.metadata.errors import HasPropertyWith, HasType, NoExtraKeys, is_string

from .property_metadata import PropertyMetadata
from .reversible_property_metadata import ReversiblePropertyMetadata


class CartesianProductMetadata(ReversiblePropertyMetadata):
Expand All @@ -18,7 +20,7 @@ class CartesianProductMetadata(ReversiblePropertyMetadata):

# Set of names of of fields that can be included in the JSON object
# describing a cartesian product property.
allowed_fields: Set[str] = PropertyMetadata.allowed_fields | {
allowed_fields: set[str] = PropertyMetadata.allowed_fields | {
"other_collection_name",
"reverse_relationship_name",
}
Expand Down
Loading