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

DM-36108: move Ellipsis typing workaround from daf_butler #137

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/DM-36108.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Moved a module with a typing workaround for the built-in `Ellipsis` (`...`) singleton here, from `daf_butler`.
38 changes: 38 additions & 0 deletions python/lsst/utils/ellipsis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This file is part of utils.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# Use of this source code is governed by a 3-clause BSD-style
# license that can be found in the LICENSE file.

from __future__ import annotations

__all__ = ("Ellipsis", "EllipsisType")

from typing import TYPE_CHECKING

if TYPE_CHECKING:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's expected for codecov to complain about this block, because by design it's only ever seen by MyPy and other type-checkers: we don't actually want it to run in tests.

# Workaround for `...` not having an exposed type in Python, borrowed from
# https://github.com/python/typing/issues/684#issuecomment-548203158
# Along with that, we need to either use `Ellipsis` instead of `...` for
# the actual sentinal value internally, and tell MyPy to ignore conversions
# from `...` to `Ellipsis` at the public-interface boundary.
#
# `Ellipsis` and `EllipsisType` should be directly imported from this
# module by related code that needs them; hopefully that will stay confined
# to `lsst.daf.butler.registry`. Putting these in __all__ is bad for
# Sphinx, and probably more confusing than helpful overall.
from enum import Enum

class EllipsisType(Enum):
Ellipsis = "..."

Ellipsis = EllipsisType.Ellipsis

else:
EllipsisType = type(Ellipsis)
Ellipsis = Ellipsis
4 changes: 2 additions & 2 deletions python/lsst/utils/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import types
from collections.abc import Mapping
from functools import lru_cache
from typing import Any, Dict, Tuple, Type
from typing import Any, Dict, Optional, Tuple, Type

import yaml

Expand Down Expand Up @@ -147,7 +147,7 @@ def getPythonPackages() -> Dict[str, str]:
return packages


_eups = None # Singleton Eups object
_eups: Optional[Any] = None # Singleton Eups object


@lru_cache(maxsize=1)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_ellipsis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is part of utils.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# Use of this source code is governed by a 3-clause BSD-style
# license that can be found in the LICENSE file.

import unittest

import lsst.utils
import lsst.utils.tests
from lsst.utils.ellipsis import Ellipsis, EllipsisType


class EllipsisTestCase(lsst.utils.tests.TestCase):
def test_ellipsis(self):
# These are true at runtime because of typing.TYPE_CHECKING guards in
# the module. When MyPy or other type-checkers run, these assertions
# would not be true, and `Ellipsis` must be used instead of the literal
# `...` to be understood by mypy.
self.assertIs(Ellipsis, ...)
self.assertIs(EllipsisType, type(...))


if __name__ == "__main__":
unittest.main()