From c27d295ee6717f3d42b9e58269fc827ba27d6b03 Mon Sep 17 00:00:00 2001 From: Dmitry Kalinkin Date: Fri, 17 Jan 2025 05:00:03 -0500 Subject: [PATCH] fix: support `const` in typename (#1231) * 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 Co-authored-by: Ianna Osborne --- src/uproot/interpretation/identify.py | 27 ++------------------------- tests/test_1229_const_in_typename.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 tests/test_1229_const_in_typename.py diff --git a/src/uproot/interpretation/identify.py b/src/uproot/interpretation/identify.py index 14c5fb150..20c3c5b92 100644 --- a/src/uproot/interpretation/identify.py +++ b/src/uproot/interpretation/identify.py @@ -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": @@ -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, @@ -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) diff --git a/tests/test_1229_const_in_typename.py b/tests/test_1229_const_in_typename.py new file mode 100644 index 000000000..3a5652bb3 --- /dev/null +++ b/tests/test_1229_const_in_typename.py @@ -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*")