Visitar URL original
Fix Node.warn crash for items without a line number by Lushiyao5 · Pull Request #15358 · pytest-dev/pytest · GitHub
Skip to content

Fix Node.warn crash for items without a line number - #15358

Open
Lushiyao5 wants to merge 2 commits into
pytest-dev:mainfrom
Lushiyao5:fix/node-warn-no-lineno-20261108
Open

Lushiyao5 wants to merge 2 commits into
pytest-dev:mainfrom
Lushiyao5:fix/node-warn-no-lineno-20261108

Conversation

@Lushiyao5

Copy link
Copy Markdown

Node.warn() crashes with AssertionError when called on a non-Python item that does not override reportinfo().

The default reportinfo() returns (self.path, None, ""), so get_fslocation_from_item() yields lineno=None. Node.warn() then hits assert lineno is not None and raises, preventing the warning from being emitted at all.

This affects custom/non-Python test items (documented in Non-Python tests) that call self.warn() during runtest() or collection.

The fix removes the assertion and falls back to lineno=0 when lineno is None, consistent with the existing -1 sentinel used by get_fslocation_from_item() (which becomes 0 after the +1 adjustment). This matches the approach already accepted in #14855 for a similar None lineno issue in reports.py.

A regression test adds a custom .custom item with default reportinfo() that calls self.warn() in runtest(), verifying the warning is emitted normally instead of crashing.

Node.warn asserted that get_fslocation_from_item returned a non-None
lineno, but non-Python items that do not override reportinfo() report
None, so calling node.warn() raised AssertionError. Fall back to lineno 0
(consistent with the -1 sentinel used by get_fslocation_from_item) so the
warning is emitted normally.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 9, 2026
@Lushiyao5

Copy link
Copy Markdown
Author

Disclosure note: this PR was diagnosed and implemented with the help of an AI coding agent; I verified the diagnosis, ran the regression test red-then-green locally (test_nodes.py 16 passed, related suites 183 passed), and applied the repo's ruff lint/format checks myself. Apologies for not stating this in the original description - #15359's closure made the expectation explicit, and I've adopted it.

The fix itself stands as reviewed: Node.warn() asserted a non-None lineno, but the default reportinfo() legitimately returns lineno=None for non-Python items, so the warning could never be emitted. If maintainers consider a different layer or test shape more appropriate (as with the #15361 alternative for #15359), happy to rework.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant