Coverage for python/lsst/daf/relation/sql/_payload.py: 95%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-27 10:10 +0000

1# This file is part of daf_relation. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("Payload",) 

25 

26import dataclasses 

27from typing import Generic, TypeVar 

28 

29import sqlalchemy 

30 

31from .._columns import ColumnTag 

32 

33_L = TypeVar("_L") 

34 

35 

36@dataclasses.dataclass(eq=False) 

37class Payload(Generic[_L]): 

38 """A struct that represents a SQL table or simple ``SELECT`` query via 

39 SQLAlchemy objects. 

40 """ 

41 

42 from_clause: sqlalchemy.sql.FromClause 

43 """SQLAlchemy representation of the FROM clause or table 

44 (`sqlalchemy.sql.FromClause`). 

45 """ 

46 

47 where: list[sqlalchemy.sql.ColumnElement] = dataclasses.field(default_factory=list) 

48 """SQLAlchemy representation of the WHERE clause, as a sequence of 

49 boolean expressions to be combined with ``AND`` 

50 (`Sequence` [ `sqlalchemy.sql.ColumnElement` ]). 

51 """ 

52 

53 columns_available: dict[ColumnTag, _L] = dataclasses.field(default_factory=dict) 

54 """Mapping from `.ColumnTag` to logical column for the columns available 

55 from the FROM clause (`dict`). 

56 """ 

57 

58 def copy(self) -> Payload[_L]: 

59 """Return a copy of this struct that can safely be modified. 

60 

61 This method takes care to copy mutable attributes while leaving 

62 immutable objects alone. 

63 """ 

64 return dataclasses.replace( 

65 self, where=list(self.where), columns_available=dict(self.columns_available) 

66 )