Skip to content

Conversation

@srothh
Copy link
Contributor

@srothh srothh commented Jul 7, 2025

Use inspect to dynamically compute the correct line number instead of hard coding it.

Fixes GH-4454
#4454

Changed the loguru test_just_log test to use inspection logic for the line number instead of hard coding it.

Use inspect to dynamically compute the correct line number instead of hard coding it.

Fixes GH-4454
@srothh srothh requested a review from a team as a code owner July 7, 2025 14:57
@srothh srothh requested a review from szokeasaurusrex July 7, 2025 14:57
Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ngl this is kinda cool, but it is really unclear to me how exactly this is supposed to work. I would want to see more code comments explaining how this logic works before going with the introspection solution.

I'm also unsure whether this fundamentally would solve the maintainability problem. Suppose some additional lines were added/removed between the inspect.currentframe() call and the call to the logger. Could the line number change in that case?

Honestly, I think it might be best to go with the simpler solution of writing a regex to ensure that the line number is present and valid, e.g. it matches \d+.


frame = inspect.currentframe()
assert frame is not None
log_line_number = frame.f_lineno + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it plus 1 here?

Replaced the inspection logic for the line number with a simple regex pattern that checks for the presence of an integer.
@codecov
Copy link

codecov bot commented Jul 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.63%. Comparing base (9e1cedd) to head (98422f9).

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4552      +/-   ##
==========================================
- Coverage   80.63%   80.63%   -0.01%     
==========================================
  Files         156      156              
  Lines       16482    16482              
  Branches     2802     2802              
==========================================
- Hits        13291    13290       -1     
+ Misses       2308     2305       -3     
- Partials      883      887       +4     

see 5 files with indirect coverage changes

assert breadcrumb["category"] == "tests.integrations.loguru.test_loguru"
assert breadcrumb["message"][23:] == formatted_message
# Check that the message matches the expected pattern with a line number
assert re.search(expected_pattern, breadcrumb["message"][23:])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want to use fullmatch rather than search. With search, the assertion passes if the string contains something matching the regex, but here, we instead want to ensure that the entire string matches

Make loguru test match against the pattern using fullmatch, so that the whole string is verified against the regex.

Fixes GH-4454
@srothh srothh merged commit a7b2d67 into master Jul 8, 2025
151 of 152 checks passed
@srothh srothh deleted the srothh/remove-loguru-hardcoded-line-number branch July 8, 2025 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove hardcoded line number in Loguru tests

3 participants