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

DocTest sorts by lineno which may be int or None #94722

Closed
Strilanc opened this issue Jul 10, 2022 · 2 comments · Fixed by #112385
Closed

DocTest sorts by lineno which may be int or None #94722

Strilanc opened this issue Jul 10, 2022 · 2 comments · Fixed by #112385
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Strilanc
Copy link

Strilanc commented Jul 10, 2022

In doctest.py the following ordering is defined for the class DocTest:

    def __lt__(self, other):
        if not isinstance(other, DocTest):
            return NotImplemented
        return ((self.name, self.filename, self.lineno, id(self))
                <
                (other.name, other.filename, other.lineno, id(other)))

This is incorrect because the lineno field may be an integer and may be None, and comparisons between integers and None fail. Typically lineno is an integer, but _find_lineno explicitly can fall back to returning None so the field may be None:

    def _find_lineno(self, obj, source_lines):
        ...

        # We couldn't find the line number.
        return None

Linked PRs

@Strilanc Strilanc added the type-bug An unexpected behavior, bug, or error label Jul 10, 2022
@pochmann
Copy link
Contributor

Can that happen despite equal names and equal filenames?

@Strilanc
Copy link
Author

Strilanc commented Jul 10, 2022

I'm reporting this because I actually was getting 'int < None not valid' exceptions from this comparison. I would have included a repro but the way i was triggering it was complex enough that it really wasn't worth the time. It involved pybind, installing from sdist not wheel, machine architecture detection, patching in a __test__ dictionary with fake entries to work around properties not being tested, and more! And I wasn't able to get the bug happening consistently outside of one specific dev environment, so even if I'd given the repro I suspected it wouldn't necessarily happen on other machines.

Even if that "names same implies lineno not null" invariant did in fact hold, that invariant is pretty complicated. It would need to be documented. Much easier to just replace self.lineno with self.lineno or -1 in the tuple.

iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 24, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 24, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 24, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 24, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Nov 25, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 25, 2023
…side (pythonGH-112385)

(cherry picked from commit fbb9027)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 25, 2023
…side (pythonGH-112385)

(cherry picked from commit fbb9027)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue Nov 25, 2023
… side (GH-112385) (#112401)

gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385)
(cherry picked from commit fbb9027)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue Nov 25, 2023
… side (GH-112385) (#112400)

gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385)
(cherry picked from commit fbb9027)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants