Skip to content

Commit

Permalink
Avoid string types with ClassVar
Browse files Browse the repository at this point in the history
As of Python 3.9.8, this does not work with Pydantic BaseModel, and
does not work in a way that causes the whole module to fail to
import.  See pydantic/pydantic#3401.

In general, Pydantic does not work well with string types as enabled
by from __future__ import annotations, which is why making that
feature the default has been delayed.  See
https://lwn.net/Articles/858576/ for more information.

Fix this by removing from __future__ import annotations from the
files using ClassVar and explicitly quote the type arguments that
contain forward references (all of which are outside the scope of
what Pydantic cares about).
  • Loading branch information
rra committed Nov 11, 2021
1 parent d67b257 commit 90b8334
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
14 changes: 6 additions & 8 deletions python/lsst/daf/butler/core/logging.py
Expand Up @@ -19,8 +19,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

__all__ = ("ButlerMDC", "ButlerLogRecords", "ButlerLogRecordHandler",
"ButlerLogRecord", "JsonLogFormatter")

Expand Down Expand Up @@ -192,7 +190,7 @@ class Config:
allow_mutation = False

@classmethod
def from_record(cls, record: LogRecord) -> ButlerLogRecord:
def from_record(cls, record: LogRecord) -> "ButlerLogRecord":
"""Create a new instance from a `~logging.LogRecord`.
Parameters
Expand Down Expand Up @@ -274,7 +272,7 @@ class ButlerLogRecords(BaseModel):
_log_format: Optional[str] = PrivateAttr(None)

@classmethod
def from_records(cls, records: Iterable[ButlerLogRecord]) -> ButlerLogRecords:
def from_records(cls, records: Iterable[ButlerLogRecord]) -> "ButlerLogRecords":
"""Create collection from iterable.
Parameters
Expand All @@ -285,7 +283,7 @@ def from_records(cls, records: Iterable[ButlerLogRecord]) -> ButlerLogRecords:
return cls(__root__=list(records))

@classmethod
def from_file(cls, filename: str) -> ButlerLogRecords:
def from_file(cls, filename: str) -> "ButlerLogRecords":
"""Read records from file.
Parameters
Expand Down Expand Up @@ -354,7 +352,7 @@ def _detect_model(startdata: Union[str, bytes]) -> bool:
return False

@classmethod
def from_stream(cls, stream: IO) -> ButlerLogRecords:
def from_stream(cls, stream: IO) -> "ButlerLogRecords":
"""Read records from I/O stream.
Parameters
Expand Down Expand Up @@ -391,7 +389,7 @@ def from_stream(cls, stream: IO) -> ButlerLogRecords:
return cls.from_records(records)

@classmethod
def from_raw(cls, serialized: Union[str, bytes]) -> ButlerLogRecords:
def from_raw(cls, serialized: Union[str, bytes]) -> "ButlerLogRecords":
"""Parse raw serialized form and return records.
Parameters
Expand Down Expand Up @@ -464,7 +462,7 @@ def __iter__(self) -> Iterator[ButlerLogRecord]: # type: ignore
def __setitem__(self, index: int, value: Record) -> None:
self.__root__[index] = self._validate_record(value)

def __getitem__(self, index: Union[slice, int]) -> Union[ButlerLogRecords, ButlerLogRecord]:
def __getitem__(self, index: Union[slice, int]) -> "Union[ButlerLogRecords, ButlerLogRecord]":
# Handles slices and returns a new collection in that
# case.
item = self.__root__[index]
Expand Down
4 changes: 1 addition & 3 deletions python/lsst/daf/butler/core/serverModels.py
Expand Up @@ -19,8 +19,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

__all__ = (
"QueryDatasetsModel",
"QueryDataIdsModel",
Expand Down Expand Up @@ -116,7 +114,7 @@ def expression(self) -> Any:
return expression

@classmethod
def from_expression(cls, expression: Any) -> ExpressionQueryParameter:
def from_expression(cls, expression: Any) -> "ExpressionQueryParameter":
"""Convert a standard dataset type expression to wire form."""
if expression is ...:
return cls()
Expand Down

0 comments on commit 90b8334

Please sign in to comment.