Skip to content

Commit 2bd05b6

Browse files
committed
Use module-level import of tempfile
1 parent a53aaa0 commit 2bd05b6

File tree

9 files changed

+38
-38
lines changed

9 files changed

+38
-38
lines changed

tests/data-files/get_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import argparse
77
import json
88
from subprocess import call
9-
from tempfile import TemporaryDirectory
9+
import tempfile
1010
from typing import Any, Dict, List, Optional
1111
from urllib.error import HTTPError
1212

@@ -50,7 +50,7 @@ def remove_bad_collection(js: Dict[str, Any]) -> Dict[str, Any]:
5050

5151
examples_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "examples"))
5252

53-
with TemporaryDirectory() as tmp_dir:
53+
with tempfile.TemporaryDirectory() as tmp_dir:
5454
call(
5555
[
5656
"git",

tests/extensions/test_label.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os
33
import unittest
4-
from tempfile import TemporaryDirectory
4+
import tempfile
55

66
import pystac
77
from pystac import Catalog, Item, CatalogType
@@ -86,7 +86,7 @@ def test_validate_label(self) -> None:
8686
label_example_1_dict, pystac.STACObjectType.ITEM
8787
)
8888

89-
with TemporaryDirectory() as tmp_dir:
89+
with tempfile.TemporaryDirectory() as tmp_dir:
9090
cat_dir = os.path.join(tmp_dir, "catalog")
9191
catalog = TestCases.test_case_1()
9292
catalog.normalize_and_save(cat_dir, catalog_type=CatalogType.SELF_CONTAINED)

tests/test_catalog.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import json
3-
from tempfile import TemporaryDirectory
3+
import tempfile
44
from typing import Any, Dict, List, Tuple, Union, cast
55
import unittest
66
from datetime import datetime
@@ -29,7 +29,7 @@
2929
class CatalogTypeTest(unittest.TestCase):
3030
def test_determine_type_for_absolute_published(self) -> None:
3131
cat = TestCases.test_case_1()
32-
with TemporaryDirectory() as tmp_dir:
32+
with tempfile.TemporaryDirectory() as tmp_dir:
3333
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.ABSOLUTE_PUBLISHED)
3434
cat_json = pystac.StacIO.default().read_json(
3535
os.path.join(tmp_dir, "catalog.json")
@@ -40,7 +40,7 @@ def test_determine_type_for_absolute_published(self) -> None:
4040

4141
def test_determine_type_for_relative_published(self) -> None:
4242
cat = TestCases.test_case_2()
43-
with TemporaryDirectory() as tmp_dir:
43+
with tempfile.TemporaryDirectory() as tmp_dir:
4444
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.RELATIVE_PUBLISHED)
4545
cat_json = pystac.StacIO.default().read_json(
4646
os.path.join(tmp_dir, "catalog.json")
@@ -68,7 +68,7 @@ def test_determine_type_for_unknown(self) -> None:
6868

6969
class CatalogTest(unittest.TestCase):
7070
def test_create_and_read(self) -> None:
71-
with TemporaryDirectory() as tmp_dir:
71+
with tempfile.TemporaryDirectory() as tmp_dir:
7272
cat_dir = os.path.join(tmp_dir, "catalog")
7373
catalog = TestCases.test_case_1()
7474

@@ -288,7 +288,7 @@ def test_clone_generates_correct_links(self) -> None:
288288
def test_save_uses_previous_catalog_type(self) -> None:
289289
catalog = TestCases.test_case_1()
290290
assert catalog.catalog_type == CatalogType.SELF_CONTAINED
291-
with TemporaryDirectory() as tmp_dir:
291+
with tempfile.TemporaryDirectory() as tmp_dir:
292292
catalog.normalize_hrefs(tmp_dir)
293293
href = catalog.self_href
294294
catalog.save()
@@ -365,7 +365,7 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:
365365

366366
catalog.generate_subcatalogs("${year}/${day}")
367367

368-
with TemporaryDirectory() as tmp_dir:
368+
with tempfile.TemporaryDirectory() as tmp_dir:
369369
catalog.normalize_hrefs(tmp_dir)
370370
catalog.save(pystac.CatalogType.SELF_CONTAINED)
371371

@@ -494,7 +494,7 @@ def item_mapper(item: pystac.Item) -> pystac.Item:
494494
item.properties["ITEM_MAPPER"] = "YEP"
495495
return item
496496

497-
with TemporaryDirectory() as tmp_dir:
497+
with tempfile.TemporaryDirectory() as tmp_dir:
498498
catalog = TestCases.test_case_1()
499499

500500
new_cat = catalog.map_items(item_mapper)
@@ -518,7 +518,7 @@ def item_mapper(item: pystac.Item) -> List[pystac.Item]:
518518
item2.properties["ITEM_MAPPER_2"] = "YEP"
519519
return [item, item2]
520520

521-
with TemporaryDirectory() as tmp_dir:
521+
with tempfile.TemporaryDirectory() as tmp_dir:
522522
catalog = TestCases.test_case_1()
523523
catalog_items = catalog.get_all_items()
524524

@@ -623,7 +623,7 @@ def asset_mapper(key: str, asset: pystac.Asset) -> pystac.Asset:
623623

624624
return asset
625625

626-
with TemporaryDirectory() as tmp_dir:
626+
with tempfile.TemporaryDirectory() as tmp_dir:
627627
catalog = TestCases.test_case_2()
628628

629629
new_cat = catalog.map_assets(asset_mapper)
@@ -656,7 +656,7 @@ def asset_mapper(
656656
else:
657657
return asset
658658

659-
with TemporaryDirectory() as tmp_dir:
659+
with tempfile.TemporaryDirectory() as tmp_dir:
660660
catalog = TestCases.test_case_2()
661661

662662
new_cat = catalog.map_assets(asset_mapper)
@@ -696,7 +696,7 @@ def asset_mapper(
696696
else:
697697
return asset
698698

699-
with TemporaryDirectory() as tmp_dir:
699+
with tempfile.TemporaryDirectory() as tmp_dir:
700700
catalog = TestCases.test_case_2()
701701

702702
new_cat = catalog.map_assets(asset_mapper)
@@ -771,7 +771,7 @@ def check_all_absolute(cat: Catalog) -> None:
771771
test_cases = TestCases.all_test_catalogs()
772772

773773
for catalog in test_cases:
774-
with TemporaryDirectory() as tmp_dir:
774+
with tempfile.TemporaryDirectory() as tmp_dir:
775775
c2 = catalog.full_copy()
776776
c2.normalize_hrefs(tmp_dir)
777777
c2.catalog_type = CatalogType.RELATIVE_PUBLISHED
@@ -797,7 +797,7 @@ def test_extra_fields(self) -> None:
797797

798798
catalog.extra_fields["custom_field"] = "Special content"
799799

800-
with TemporaryDirectory() as tmp_dir:
800+
with tempfile.TemporaryDirectory() as tmp_dir:
801801
p = os.path.join(tmp_dir, "catalog.json")
802802
catalog.save_object(include_self_link=False, dest_href=p)
803803
with open(p) as f:
@@ -822,7 +822,7 @@ def test_validate_all(self) -> None:
822822
item = cat.get_item("area-1-1-labels", recursive=True)
823823
assert item is not None
824824
item.geometry = {"type": "INVALID", "coordinates": "NONE"}
825-
with TemporaryDirectory() as tmp_dir:
825+
with tempfile.TemporaryDirectory() as tmp_dir:
826826
cat.normalize_hrefs(tmp_dir)
827827
cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)
828828

@@ -843,7 +843,7 @@ def test_set_hrefs_manually(self) -> None:
843843
year += 1
844844
month += 1
845845

846-
with TemporaryDirectory() as tmp_dir:
846+
with tempfile.TemporaryDirectory() as tmp_dir:
847847
for root, _, items in catalog.walk():
848848

849849
# Set root's HREF based off the parent
@@ -933,7 +933,7 @@ def test_reading_iterating_and_writing_works_as_expected(self) -> None:
933933
for item in cat.get_all_items():
934934
pass
935935

936-
with TemporaryDirectory() as tmp_dir:
936+
with tempfile.TemporaryDirectory() as tmp_dir:
937937
new_stac_uri = os.path.join(tmp_dir, "test-case-6")
938938
cat.normalize_hrefs(new_stac_uri)
939939
cat.save(catalog_type=CatalogType.SELF_CONTAINED)
@@ -1003,7 +1003,7 @@ def check_catalog(self, c: Catalog, tag: str) -> None:
10031003
self.check_item(item, tag)
10041004

10051005
def test_full_copy_1(self) -> None:
1006-
with TemporaryDirectory() as tmp_dir:
1006+
with tempfile.TemporaryDirectory() as tmp_dir:
10071007
cat = Catalog(id="test", description="test catalog")
10081008

10091009
item = Item(
@@ -1024,7 +1024,7 @@ def test_full_copy_1(self) -> None:
10241024
self.check_catalog(cat2, "dest")
10251025

10261026
def test_full_copy_2(self) -> None:
1027-
with TemporaryDirectory() as tmp_dir:
1027+
with tempfile.TemporaryDirectory() as tmp_dir:
10281028
cat = Catalog(id="test", description="test catalog")
10291029
image_item = Item(
10301030
id="Imagery",
@@ -1071,7 +1071,7 @@ def test_full_copy_2(self) -> None:
10711071
self.check_catalog(cat2, "dest")
10721072

10731073
def test_full_copy_3(self) -> None:
1074-
with TemporaryDirectory() as tmp_dir:
1074+
with tempfile.TemporaryDirectory() as tmp_dir:
10751075
root_cat = TestCases.test_case_1()
10761076
root_cat.normalize_hrefs(
10771077
os.path.join(tmp_dir, "catalog-full-copy-3-source")
@@ -1085,7 +1085,7 @@ def test_full_copy_3(self) -> None:
10851085
self.check_catalog(cat2, "dest")
10861086

10871087
def test_full_copy_4(self) -> None:
1088-
with TemporaryDirectory() as tmp_dir:
1088+
with tempfile.TemporaryDirectory() as tmp_dir:
10891089
root_cat = TestCases.test_case_2()
10901090
root_cat.normalize_hrefs(
10911091
os.path.join(tmp_dir, "catalog-full-copy-4-source")

tests/test_collection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
from datetime import datetime
55
from dateutil import tz
6-
from tempfile import TemporaryDirectory
6+
import tempfile
77

88
import pystac
99
from pystac.extensions.eo import EOExtension
@@ -35,7 +35,7 @@ def test_save_uses_previous_catalog_type(self) -> None:
3535
collection = TestCases.test_case_8()
3636
assert collection.STAC_OBJECT_TYPE == pystac.STACObjectType.COLLECTION
3737
self.assertEqual(collection.catalog_type, CatalogType.SELF_CONTAINED)
38-
with TemporaryDirectory() as tmp_dir:
38+
with tempfile.TemporaryDirectory() as tmp_dir:
3939
collection.normalize_hrefs(tmp_dir)
4040
href = collection.self_href
4141
collection.save()
@@ -84,7 +84,7 @@ def test_extra_fields(self) -> None:
8484

8585
collection.extra_fields["test"] = "extra"
8686

87-
with TemporaryDirectory() as tmp_dir:
87+
with tempfile.TemporaryDirectory() as tmp_dir:
8888
p = os.path.join(tmp_dir, "collection.json")
8989
collection.save_object(include_self_link=False, dest_href=p)
9090
with open(p) as f:

tests/test_item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from datetime import datetime
33
import json
4-
from tempfile import TemporaryDirectory
4+
import tempfile
55
from typing import Any, Dict, List
66
import unittest
77

@@ -73,7 +73,7 @@ def test_extra_fields(self) -> None:
7373

7474
item.extra_fields["test"] = "extra"
7575

76-
with TemporaryDirectory() as tmp_dir:
76+
with tempfile.TemporaryDirectory() as tmp_dir:
7777
p = os.path.join(tmp_dir, "item.json")
7878
item.save_object(include_self_link=False, dest_href=p)
7979
with open(p) as f:

tests/test_stac_io.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import unittest
33
import warnings
4-
from tempfile import TemporaryDirectory
4+
import tempfile
55

66
import pystac
77
from pystac.stac_io import STAC_IO
@@ -26,14 +26,14 @@ def test_read_write_collection(self) -> None:
2626
collection = pystac.read_file(
2727
TestCases.get_path("data-files/collections/multi-extent.json")
2828
)
29-
with TemporaryDirectory() as tmp_dir:
29+
with tempfile.TemporaryDirectory() as tmp_dir:
3030
dest_href = os.path.join(tmp_dir, "collection.json")
3131
pystac.write_file(collection, dest_href=dest_href)
3232
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
3333

3434
def test_read_item(self) -> None:
3535
item = pystac.read_file(TestCases.get_path("data-files/item/sample-item.json"))
36-
with TemporaryDirectory() as tmp_dir:
36+
with tempfile.TemporaryDirectory() as tmp_dir:
3737
dest_href = os.path.join(tmp_dir, "item.json")
3838
pystac.write_file(item, dest_href=dest_href)
3939
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
@@ -42,7 +42,7 @@ def test_read_write_catalog(self) -> None:
4242
catalog = pystac.read_file(
4343
TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
4444
)
45-
with TemporaryDirectory() as tmp_dir:
45+
with tempfile.TemporaryDirectory() as tmp_dir:
4646
dest_href = os.path.join(tmp_dir, "catalog.json")
4747
pystac.write_file(catalog, dest_href=dest_href)
4848
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")

tests/test_writing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from tempfile import TemporaryDirectory
2+
import tempfile
33
from typing import Any, List
44

55
import pystac
@@ -105,7 +105,7 @@ def validate_catalog_link_type(
105105
def do_test(
106106
self, catalog: pystac.Catalog, catalog_type: pystac.CatalogType
107107
) -> None:
108-
with TemporaryDirectory() as tmp_dir:
108+
with tempfile.TemporaryDirectory() as tmp_dir:
109109
catalog.normalize_hrefs(tmp_dir)
110110
self.validate_catalog(catalog)
111111

tests/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from tests.utils.stac_io_mock import MockStacIO
1818

1919
if TYPE_CHECKING:
20-
from tempfile import TemporaryDirectory as TemporaryDirectory_Type
20+
import tempfile as TemporaryDirectory_Type
2121

2222

2323
def assert_to_from_dict(

tests/validation/test_validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pystac.utils import get_opt
66
import shutil
77
import unittest
8-
from tempfile import TemporaryDirectory
8+
import tempfile
99

1010
import jsonschema
1111

@@ -99,7 +99,7 @@ def test_validate_all(self) -> None:
9999
# Modify a 0.8.1 collection in a catalog to be invalid with a
100100
# since-renamed extension and make sure it catches the validation error.
101101

102-
with TemporaryDirectory() as tmp_dir:
102+
with tempfile.TemporaryDirectory() as tmp_dir:
103103
dst_dir = os.path.join(tmp_dir, "catalog")
104104
# Copy test case 7 to the temporary directory
105105
catalog_href = get_opt(TestCases.test_case_7().get_self_href())

0 commit comments

Comments
 (0)