Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Doctest incorrectly locates a decorated function #115392

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@chaudhary1337

Description

@chaudhary1337

Bug report

Bug description:

TL;DR: If a function is decorated, the doctest is unable to find the correct location of the function.

Example

Consider two simple files,main.py anddecorate.py.

Contents ofmain.py:

fromdecorateimportdecorator@decoratordeffoo():"""    >>> foo()    2    """return42

Contents ofdecorate.py:

importfunctoolsdefdecorator(f):@functools.wraps(f)definner():returnf()returninner

If we run a doctest like so:python3 -m doctest main.py, we find the errorcorrectly on the line number 7, the line which says>>> foo(). Traceback is output as follows.

**********************************************************************File "/codemill/chaudhat/learning/demo/main.py", line 7, in main.fooFailed example:    foo()Expected:    2Got:    42**********************************************************************1 items had failures:   1 of   2 in main.foo***Test Failed*** 1 failures.

Incorrect Output

However, if we move thedecorator definition in thedecorate.py file by a few lines, as shown, (the space between could be empty/defining a function, etc.), we see that the doctest is unable to find the location of the decorated function,foo, and just outputs? as the line number.

importfunctoolsdefdecorator(f):@functools.wraps(f)definner():returnf()returninner

Traceback:

**********************************************************************File "/codemill/chaudhat/learning/demo/main.py", line ?, in main.fooFailed example:    foo()Expected:    2Got:    42**********************************************************************1 items had failures:   1 of   1 in main.foo***Test Failed*** 1 failures.

PS: If move thedecorator definition by even a line up, it shows that the line,>>> foo() incorrectly lives on line 10 and not line 7.

Why?

The "?" is printed simply because while doctest is able to find the example's lineno, it is unable to understand the test's lineno. I found this after printing out the line numbers in the_failure_header function indoctest.py.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp