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-38492: Move ResourcePathExpression definition to end of file #53

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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-38492.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* ``ResourcePathExpression`` can now be used in an `isinstance()` call on Python 3.10 and newer.
10 changes: 5 additions & 5 deletions python/lsst/resources/_resourcePath.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
MAX_WORKERS = 10


ResourcePathExpression = Union[str, urllib.parse.ParseResult, "ResourcePath", Path]
"""Type-annotation alias for objects that can be coerced to ResourcePath.
"""


class ResourcePath:
"""Convenience wrapper around URI parsers.

Expand Down Expand Up @@ -1404,3 +1399,8 @@ def _openImpl(
out_bytes = str_buffer.getvalue().encode(encoding)
if "r" not in mode or "+" in mode:
self.write(out_bytes, overwrite=("x" not in mode))


ResourcePathExpression = Union[str, urllib.parse.ParseResult, ResourcePath, Path]
"""Type-annotation alias for objects that can be coerced to ResourcePath.
"""
23 changes: 22 additions & 1 deletion tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@

import os
import pathlib
import sys
import unittest
import unittest.mock
import urllib.parse

from lsst.resources import ResourcePath
from lsst.resources import ResourcePath, ResourcePathExpression
from lsst.resources.tests import GenericReadWriteTestCase, GenericTestCase

TESTDIR = os.path.abspath(os.path.dirname(__file__))

_OLD_PYTHON = False
if sys.version_info < (3, 10, 0):
_OLD_PYTHON = True


class SimpleTestCase(unittest.TestCase):
@unittest.skipIf(_OLD_PYTHON, "isinstance() with unions is not supported.")
def test_instance(self):
for example in (
"xxx",
ResourcePath("xxx"),
pathlib.Path("xxx"),
urllib.parse.urlparse("file:///xxx"),
):
self.assertIsInstance(example, ResourcePathExpression)

for example in ({1, 2, 3}, 42, self):
self.assertNotIsInstance(example, ResourcePathExpression)


class FileTestCase(GenericTestCase, unittest.TestCase):
scheme = "file"
Expand Down