Skip to content

Commit

Permalink
fix: support const in typename (#1231)
Browse files Browse the repository at this point in the history
* add tests/test_1229_const_in_typename.py

* tests/test_1229_const_in_typename.py: test without a file

* ignore the const token

* workaround test failure

When full test suite is ran the following error is seen:
```
____________________________ test_const_in_typename ____________________________

    def test_const_in_typename():
>       assert parse_typename("TH1I*") == AsPointer(Model_TH1I)
E       AssertionError: assert AsPointer(Model_TH1I) == AsPointer(Model_TH1I)
E        +  where AsPointer(Model_TH1I) = parse_typename('TH1I*')
E        +  and   AsPointer(Model_TH1I) = AsPointer(Model_TH1I)

tests/test_1229_const_in_typename.py:12: AssertionError
```

Running just the `tests/test_1229_const_in_typename.py` doesn't show that.
Not clear what re-defines the types/modules.

* add back the file-based test (since it was added to skhep_testdata)

* style: pre-commit fixes

* checking for 'const' early makes these code blocks dead code

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: jpivarski <[email protected]>
Co-authored-by: Ianna Osborne <[email protected]>
  • Loading branch information
4 people authored Jan 17, 2025
1 parent 0a5c76e commit c27d295
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/uproot/interpretation/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ def _parse_node(tokens, i, typename, file, quote, header, inner_header):
if tokens[i].group(0) == ",":
_parse_error(tokens[i].start() + 1, typename, file)

elif tokens[i].group(0) == "const":
return _parse_node(tokens, i + 1, typename, file, quote, header, inner_header)
elif tokens[i].group(0) == "Bool_t":
return i + 1, _parse_maybe_quote('numpy.dtype("?")', quote)
elif tokens[i].group(0) == "bool":
Expand Down Expand Up @@ -919,19 +921,6 @@ def _parse_node(tokens, i, typename, file, quote, header, inner_header):
_parse_maybe_quote(f"uproot.containers.AsString({header})", quote),
)

elif (
has2
and tokens[i].group(0) == "const"
and (
tokens[i + 1].group(0) == "string"
or _simplify_token(tokens[i + 1]) == "std::string"
)
):
return (
i + 2,
_parse_maybe_quote(f"uproot.containers.AsString({header})", quote),
)

elif tokens[i].group(0) == "TString":
return (
i + 1,
Expand All @@ -947,18 +936,6 @@ def _parse_node(tokens, i, typename, file, quote, header, inner_header):
quote,
),
)
elif (
has2
and tokens[i].group(0) == "const"
and _simplify_token(tokens[i + 1]) == "char*"
):
return (
i + 2,
_parse_maybe_quote(
"uproot.containers.AsString(False, length_bytes='4', typename='char*')",
quote,
),
)

elif tokens[i].group(0) == "bitset" or _simplify_token(tokens[i]) == "std::bitset":
_parse_expect("<", tokens, i + 1, typename, file)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_1229_const_in_typename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

import pytest
import skhep_testdata

import uproot
from uproot.interpretation.identify import parse_typename


@pytest.fixture(scope="module")
def datafile(tmpdir_factory):
yield skhep_testdata.data_path("uproot-issue-1229.root")


@pytest.fixture
def tree(datafile):
with uproot.open(datafile) as f:
yield f["tree"]


def test_const_in_typename(tree):
assert tree["branch/pointer"].typename == "TFooMember*"
assert tree["branch/const_pointer"].typename == "TFooMember*"


def test_const_parse_typename():
assert parse_typename("TH1I*") == parse_typename("const TH1I*")

0 comments on commit c27d295

Please sign in to comment.