Skip to content

Review suggestions#3

Merged
TTsangSC merged 9 commits intoTTsangSC:eager-prof-modfrom
Erotemic:review-suggestions
Jul 6, 2025
Merged

Review suggestions#3
TTsangSC merged 9 commits intoTTsangSC:eager-prof-modfrom
Erotemic:review-suggestions

Conversation

@Erotemic
Copy link

@Erotemic Erotemic commented Jul 5, 2025

Based on my review, I have a refactoring / modification of several components.

The idea is to:

  • Reduce use of functools.partial when possible
  • Decrease cognitive complexity of functions
  • Avoid setting attributes on options when possible (although I had to eventually do some of this so the refactor wouldn't blow up)
  • Greatly simplify logging - removing the feature where the code itself is dumped to the screen.

@TTsangSC
Copy link
Owner

TTsangSC commented Jul 6, 2025

Just took a cursory glance, seems that you also unnested a lot of functions. Will take a closer look later today.

One maybe unintended side effect though is that all these functions are now in the global namespace, and will show in the docs because we have in the Sphinx boilerplate

.. automodule:: <module>
   :private-members:

I wonder if we should just remove that in docs/source/auto/kernprof.rst(or even across the board), since having private members in the docs IMO gives mixed signals as to what is part of the API and what isn't.

@Erotemic
Copy link
Author

Erotemic commented Jul 6, 2025

Yeah, that probably makes sense to remove private members from docs.

('--view', # Verbosity level 1 = `--view`
{'^Output to stdout': True,
r"^Wrote .* '.*script\.py\.lprof'": True,
r'Parser output:''(?:\n)+'r'.*namespace\((?:.+,\n)*.*\)': False,
Copy link
Owner

Choose a reason for hiding this comment

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

Guess that we can do the same for the 1st parametrization.. but of course I can also just put that in after merging.

Copy link
Author

Choose a reason for hiding this comment

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

I thought that I had removed the log associated with this test. If it's a mistake you can put it back in.

STATIC_ANALYSIS = _boolean_environ('LINE_PROFILER_STATIC_ANALYSIS')

log = _logger.Logger('line_profiler')
log = _logger.Logger('line_profiler', backend='auto')
Copy link
Owner

Choose a reason for hiding this comment

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

I thought this is the default? Doesn't hurt to be explicit though, of course.

Copy link
Author

Choose a reason for hiding this comment

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

yeah, I'm just being explicit. The logger gets initialized twice when kernprof is used, which is why I changed it.

if options.rich and simple_logging:
from rich.console import Console
from rich.markup import escape
def no_op(*_, **__) -> None:
Copy link
Owner

@TTsangSC TTsangSC Jul 6, 2025

Choose a reason for hiding this comment

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

Just caught that this is the only annotated function in the entire file (my fault, it has been here since before your PR). Maybe we should remove that for consistency...

Copy link
Author

Choose a reason for hiding this comment

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

Its ok to annotate.



def _write_preimports(prof, options, exclude):
def _gather_preimport_targets(options, exclude):
Copy link
Owner

Choose a reason for hiding this comment

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

Good call breaking up _write_preimports, it was indeed getting a bit long...

'Wrote temporary module for pre-imports '
f'to {temp_mod_path!r}:',
code)
f'to {temp_mod_path!r}:')
Copy link
Owner

Choose a reason for hiding this comment

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

We should probably remove the colon now that we're no longer outputting the module to the log

after initial parsing of options; not to be invoked on its own.
"""
script_file, prof = _pre_profile(options, module)
try:
Copy link
Owner

Choose a reason for hiding this comment

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

I know this is old code but we might as well discuss this now, do you think we can either:

  • Unnest the inner try: ... except: ... block so we just have one single try: ... except: ... finally: ... block, or
  • Remove the except: ... clause so that we don't suppress those BaseExceptions?

Copy link
Author

Choose a reason for hiding this comment

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

I haven't really thought through what the implications of the double try nesting is. Looking at git blame, that is really old code from 2008, so I imagine we can spruce it up.

Copy link
Owner

@TTsangSC TTsangSC left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for the PR. If you think it's ready I'll pull, (make the aforementioned small changes), and push to pyutils#337.

@Erotemic
Copy link
Author

Erotemic commented Jul 6, 2025

I think all you should need to do is hit merge, and pyutils#337 will update.

@TTsangSC TTsangSC merged commit 0b8cc7e into TTsangSC:eager-prof-mod Jul 6, 2025
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.

2 participants