Skip to content

Commit 113c58c

Browse files
authored
chore: replace some old code (python 2) (#1020)
* add test for tree write update * rename test * move test to new file to avoid skip * remove python 2 related comment * update description * delete new test * remove comment * remove comment * remove lzma helper * remove "isstr" helper * fix annotation
1 parent 983b986 commit 113c58c

File tree

16 files changed

+61
-85
lines changed

16 files changed

+61
-85
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
mamba install root
4545
conda list
4646
47-
- name: Install sshd for sshfs tests
47+
- name: Install sshd for fsspec ssh tests
4848
if: runner.os != 'macOS' && runner.os != 'Windows'
4949
run: |
5050
sudo apt-get install -y openssh-server

src/uproot/_util.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ def isnum(x):
5656
)
5757

5858

59-
def isstr(x):
60-
"""
61-
Returns True if and only if ``x`` is a string (including Python 2 unicode).
62-
"""
63-
return isinstance(x, str)
64-
65-
6659
def ensure_str(x):
6760
"""
6861
Ensures that ``x`` is a string (decoding with 'surrogateescape' if necessary).
@@ -166,7 +159,7 @@ def regularize_filter(filter):
166159
return no_filter
167160
elif callable(filter):
168161
return filter
169-
elif isstr(filter):
162+
elif isinstance(filter, str):
170163
m = _regularize_filter_regex.match(filter)
171164
if m is not None:
172165
regex, flags = m.groups()
@@ -207,7 +200,7 @@ def regularize_rename(rename):
207200
elif callable(rename):
208201
return rename
209202

210-
elif isstr(rename):
203+
elif isinstance(rename, str):
211204
m = _regularize_filter_regex_rename.match(rename)
212205
if m is not None:
213206
regex, trans, flags = m.groups()
@@ -222,7 +215,7 @@ def regularize_rename(rename):
222215
elif isinstance(rename, Iterable) and not isinstance(rename, bytes):
223216
rules = []
224217
for x in rename:
225-
if isstr(x):
218+
if isinstance(x, str):
226219
m = _regularize_filter_regex_rename.match(x)
227220
if m is not None:
228221
regex, trans, flags = m.groups()
@@ -387,7 +380,7 @@ def file_path_to_source_class(file_path, options):
387380
return out, file_path
388381

389382
if (
390-
not isstr(file_path)
383+
not isinstance(file_path, str)
391384
and hasattr(file_path, "read")
392385
and hasattr(file_path, "seek")
393386
):
@@ -593,7 +586,7 @@ def memory_size(data, error_message=None):
593586
Regularizes strings like '## kB' and plain integer number of bytes to
594587
an integer number of bytes.
595588
"""
596-
if isstr(data):
589+
if isinstance(data, str):
597590
m = re.match(
598591
r"^\s*([+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?)\s*([kmgtpezy]?b)\s*$",
599592
data,
@@ -1001,11 +994,11 @@ def _regularize_files_inner(files, parse_colon, counter, HasBranches, steps_allo
1001994

1002995
maybe_steps = None
1003996

1004-
if isstr(files2) and not isstr(files):
997+
if isinstance(files2, str) and not isinstance(files, str):
1005998
parse_colon = False
1006999
files = files2
10071000

1008-
if isstr(files):
1001+
if isinstance(files, str):
10091002
if parse_colon:
10101003
file_path, object_path = file_object_path_split(files)
10111004
else:
@@ -1098,7 +1091,7 @@ def regularize_files(files, steps_allowed):
10981091
for file_path, object_path, maybe_steps in _regularize_files_inner(
10991092
files, True, counter, HasBranches, steps_allowed
11001093
):
1101-
if isstr(file_path):
1094+
if isinstance(file_path, str):
11021095
key = (counter[0], file_path, object_path)
11031096
if key not in seen:
11041097
out.append((file_path, object_path))
@@ -1135,7 +1128,7 @@ def regularize_object_path(
11351128
object_cache=None,
11361129
array_cache=None,
11371130
custom_classes=custom_classes,
1138-
**options, # NOTE: a comma after **options breaks Python 2
1131+
**options,
11391132
).root_directory
11401133
if object_path is None:
11411134
trees = file.keys(filter_classname="TTree", cycle=False)

src/uproot/behaviors/TBranch.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def iterate(
6363
report=False,
6464
custom_classes=None,
6565
allow_missing=False,
66-
**options, # NOTE: a comma after **options breaks Python 2
66+
**options,
6767
):
6868
"""
6969
Args:
@@ -243,7 +243,7 @@ def concatenate(
243243
how=None,
244244
custom_classes=None,
245245
allow_missing=False,
246-
**options, # NOTE: a comma after **options breaks Python 2
246+
**options,
247247
):
248248
"""
249249
Args:
@@ -1617,7 +1617,7 @@ def __getitem__(self, where):
16171617

16181618
if uproot._util.isint(where):
16191619
return self.branches[where]
1620-
elif uproot._util.isstr(where):
1620+
elif isinstance(where, str):
16211621
where = uproot._util.ensure_str(where)
16221622
else:
16231623
raise TypeError(f"where must be an integer or a string, not {where!r}")
@@ -2666,11 +2666,11 @@ def _regularize_executors(decompression_executor, interpretation_executor, file)
26662666
def _regularize_array_cache(array_cache, file):
26672667
if isinstance(array_cache, MutableMapping):
26682668
return array_cache
2669-
elif uproot._util.isstr(array_cache) and array_cache == "inherit":
2669+
elif isinstance(array_cache, str) and array_cache == "inherit":
26702670
return file._array_cache
26712671
elif array_cache is None:
26722672
return None
2673-
elif uproot._util.isint(array_cache) or uproot._util.isstr(array_cache):
2673+
elif uproot._util.isint(array_cache) or isinstance(array_cache, str):
26742674
return uproot.cache.LRUArrayCache(array_cache)
26752675
else:
26762676
raise TypeError("array_cache must be None, a MutableMapping, or a memory size")
@@ -2895,7 +2895,7 @@ def _regularize_expressions(
28952895
branchname,
28962896
)
28972897

2898-
elif uproot._util.isstr(expressions):
2898+
elif isinstance(expressions, str):
28992899
_regularize_expression(
29002900
hasbranches,
29012901
expressions,
@@ -2917,7 +2917,7 @@ def _regularize_expressions(
29172917
else:
29182918
items = []
29192919
for expression in expressions:
2920-
if uproot._util.isstr(expression):
2920+
if isinstance(expression, str):
29212921
items.append((expression, None))
29222922
elif isinstance(expression, tuple) and len(expression) == 2:
29232923
items.append(expression)
@@ -2972,7 +2972,7 @@ def _regularize_expressions(
29722972

29732973
if cut is None:
29742974
pass
2975-
elif uproot._util.isstr(cut):
2975+
elif isinstance(cut, str):
29762976
_regularize_expression(
29772977
hasbranches,
29782978
cut,

src/uproot/compression.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
as functions for compressing and decompressing a :doc:`uproot.source.chunk.Chunk`.
66
"""
77

8+
from __future__ import annotations
89

910
import struct
1011
import threading
@@ -88,7 +89,7 @@ class _DecompressZLIB:
8889
_2byte = b"ZL"
8990
_method = b"\x08"
9091

91-
def decompress(self, data, uncompressed_bytes=None):
92+
def decompress(self, data: bytes, uncompressed_bytes=None) -> bytes:
9293
import zlib
9394

9495
return zlib.decompress(data)
@@ -125,7 +126,7 @@ def level(self, value):
125126
raise ValueError("Compression level must be between 0 and 9 (inclusive)")
126127
self._level = int(value)
127128

128-
def compress(self, data):
129+
def compress(self, data: bytes) -> bytes:
129130
import zlib
130131

131132
return zlib.compress(data, self._level)
@@ -136,8 +137,9 @@ class _DecompressLZMA:
136137
_2byte = b"XZ"
137138
_method = b"\x00"
138139

139-
def decompress(self, data, uncompressed_bytes=None):
140-
lzma = uproot.extras.lzma()
140+
def decompress(self, data: bytes, uncompressed_bytes=None) -> bytes:
141+
import lzma
142+
141143
return lzma.decompress(data)
142144

143145

@@ -150,8 +152,6 @@ class LZMA(Compression, _DecompressLZMA):
150152
Represents the LZMA compression algorithm.
151153
152154
Uproot uses ``lzma`` from the Python 3 standard library.
153-
154-
In Python 2, ``backports.lzma`` must be installed.
155155
"""
156156

157157
def __init__(self, level):
@@ -174,8 +174,9 @@ def level(self, value):
174174
raise ValueError("Compression level must be between 0 and 9 (inclusive)")
175175
self._level = int(value)
176176

177-
def compress(self, data):
178-
lzma = uproot.extras.lzma()
177+
def compress(self, data: bytes) -> bytes:
178+
import lzma
179+
179180
return lzma.compress(data, preset=self._level)
180181

181182

@@ -184,7 +185,7 @@ class _DecompressLZ4:
184185
_2byte = b"L4"
185186
_method = b"\x01"
186187

187-
def decompress(self, data, uncompressed_bytes=None):
188+
def decompress(self, data: bytes, uncompressed_bytes=None) -> bytes:
188189
lz4_block = uproot.extras.lz4_block()
189190
if uncompressed_bytes is None:
190191
raise ValueError(
@@ -224,7 +225,7 @@ def level(self, value):
224225
raise ValueError("Compression level must be between 0 and 12 (inclusive)")
225226
self._level = int(value)
226227

227-
def compress(self, data):
228+
def compress(self, data: bytes) -> bytes:
228229
lz4_block = uproot.extras.lz4_block()
229230
return lz4_block.compress(data, compression=self._level, store_size=False)
230231

@@ -245,7 +246,7 @@ def decompressor(self):
245246
self._decompressor.obj = zstandard.ZstdDecompressor()
246247
return self._decompressor.obj
247248

248-
def decompress(self, data, uncompressed_bytes=None):
249+
def decompress(self, data: bytes, uncompressed_bytes=None) -> bytes:
249250
return self.decompressor.decompress(data)
250251

251252

@@ -288,7 +289,7 @@ def compressor(self):
288289
self._compressor = zstandard.ZstdCompressor(level=self._level)
289290
return self._compressor
290291

291-
def compress(self, data):
292+
def compress(self, data: bytes) -> bytes:
292293
return self.compressor.compress(data)
293294

294295

@@ -475,7 +476,7 @@ def hook_after_block(**kwargs):
475476
_4byte = struct.Struct("<I") # compressed sizes are 3-byte little endian!
476477

477478

478-
def compress(data, compression):
479+
def compress(data: bytes, compression: Compression) -> bytes:
479480
"""
480481
Args:
481482
data (bytes, memoryview, or NumPy array): Data to compress.

src/uproot/containers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _read_nested(
117117

118118

119119
def _tostring(value):
120-
if uproot._util.isstr(value):
120+
if isinstance(value, str):
121121
return repr(value)
122122
else:
123123
return str(value)

src/uproot/extras.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
error messages containing instructions on how to install the library.
99
"""
1010

11-
1211
import atexit
1312
import importlib.metadata as importlib_metadata
1413
import os
@@ -160,16 +159,6 @@ def xrootd_version():
160159
return None
161160

162161

163-
def lzma():
164-
"""
165-
Imports and returns ``lzma`` (which is part of the Python 3 standard
166-
library, but not Python 2).
167-
"""
168-
import lzma
169-
170-
return lzma
171-
172-
173162
def lz4_block():
174163
"""
175164
Imports and returns ``lz4``.

src/uproot/interpretation/library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ def group(self, arrays, expression_context, how):
867867
elif how is dict:
868868
return {_rename(name, c): arrays[name] for name, c in expression_context}
869869

870-
elif uproot._util.isstr(how) or how is None:
870+
elif isinstance(how, str) or how is None:
871871
arrays, names = _pandas_only_series(pandas, arrays, expression_context)
872872
return _pandas_memory_efficient(pandas, arrays, names)
873873

src/uproot/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ def base(self, *cls):
591591
592592
The ``cls`` arguments may be Python classes or C++ classname strings to match.
593593
"""
594-
cpp_names = [classname_regularize(x) for x in cls if uproot._util.isstr(x)]
595-
py_types = tuple(x for x in cls if not uproot._util.isstr(x))
594+
cpp_names = [classname_regularize(x) for x in cls if isinstance(x, str)]
595+
py_types = tuple(x for x in cls if not isinstance(x, str))
596596

597597
out = []
598598
for x in getattr(self, "_bases", []):
@@ -610,8 +610,8 @@ def is_instance(self, *cls):
610610
611611
The ``cls`` arguments may be Python classes or C++ classname strings to match.
612612
"""
613-
cpp_names = [classname_regularize(x) for x in cls if uproot._util.isstr(x)]
614-
py_types = tuple(x for x in cls if not uproot._util.isstr(x))
613+
cpp_names = [classname_regularize(x) for x in cls if isinstance(x, str)]
614+
py_types = tuple(x for x in cls if not isinstance(x, str))
615615

616616
if isinstance(self, py_types) or any(self.classname == n for n in cpp_names):
617617
return True

src/uproot/models/TTree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def num_entries(paths):
949949

950950
if isinstance(paths, dict):
951951
paths = list(paths.items())
952-
elif not uproot._util.isstr(paths):
952+
elif not isinstance(paths, str):
953953
paths = [(uproot._util.file_object_path_split(path)) for path in paths]
954954
else:
955955
paths = [uproot._util.file_object_path_split(paths)]

0 commit comments

Comments
 (0)