Skip to content

Commit

Permalink
Use Sequence instead of list for tables and columns
Browse files Browse the repository at this point in the history
The List type is considered immutable, which means polymorphism does
not work for typing. Instead use the Sequence type which is not
considered immutable and accepts child classes in the declaration.
  • Loading branch information
JeremyMcCormick committed Jan 29, 2024
1 parent 019da59 commit 823f4bc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions python/felis/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import logging
from collections.abc import Mapping
from enum import Enum
from typing import Any, Literal
from typing import Any, Literal, Sequence

from astropy import units as units # type: ignore
from astropy.io.votable import ucd # type: ignore
Expand Down Expand Up @@ -243,7 +243,7 @@ class ForeignKeyConstraint(Constraint):
class Table(BaseObject):
"""A database table."""

columns: list[Column]
columns: Sequence[Column]
"""The columns in the table."""

constraints: list[Constraint] = Field(default_factory=list)
Expand Down Expand Up @@ -373,7 +373,7 @@ class Schema(BaseObject):
version: SchemaVersion | str | None = None
"""The version of the schema."""

tables: list[Table]
tables: Sequence[Table]
"""The tables in the schema."""

id_map: dict[str, Any] = Field(default_factory=dict, exclude=True)
Expand Down
6 changes: 3 additions & 3 deletions python/felis/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
from typing import Any, Type
from typing import Any, Sequence, Type

from pydantic import Field, model_validator

Expand Down Expand Up @@ -51,7 +51,7 @@ class RspTable(Table):
tap_table_index: int = Field(..., alias="tap:table_index")
"""Redefine the TAP_SCHEMA table index so that it is required."""

columns: list[RspColumn] # type: ignore
columns: Sequence[RspColumn]
"""Redefine columns to include RSP validation."""

@model_validator(mode="after") # type: ignore
Expand All @@ -74,7 +74,7 @@ class RspSchema(Schema):
"""Redefine description to make it required and non-empty.
"""

tables: list[RspTable] # type: ignore
tables: Sequence[RspTable]
"""Redefine tables to include RSP validation."""

@model_validator(mode="after") # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def test_rsp_validation(self) -> None:
with self.assertRaises(ValidationError):
RspSchema(tables=[tbl])

# Creating a schema with duplicate TAP table indices should throw an exception.
# Creating a schema with duplicate TAP table indices should throw an
# exception.
with self.assertRaises(ValidationError):
RspSchema(
**{"name": "testSchema", "@id": "#test_schema_id", "description": "test schema"},
Expand Down

0 comments on commit 823f4bc

Please sign in to comment.