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

Print the line from where the issue is raised #34

Open
NicolasMICAUX opened this issue Aug 24, 2023 · 4 comments
Open

Print the line from where the issue is raised #34

NicolasMICAUX opened this issue Aug 24, 2023 · 4 comments

Comments

@NicolasMICAUX
Copy link

Hi :)
Great lib!
In the example with exceptions, do you see a way to report the line where the issue is raised?
The use case would be to understand where the issue is raised, and if one should try to handle it or not. Having the context of the "raise" line makes it easier to understand what is the error (especially with often used exception like TypeError).

example:
{'ValueError': 'Line 13,
'TypeError': 'Line 17'}

I'm not very familiar with AST but seems that one can get the attributes 'lineno', 'col_offset', 'end_lineno', 'end_col_offset' for which node. We still need to trace parents to get like where in the file is this from.

If you have an idea of how to do this, I can implement it. :)

@shadycuz
Copy link
Member

@NicolasMICAUX

I think you could modify this function:

def _add_exception(self, name: str):
        self.raw_exceptions.append(name)

        if name not in self.found_exceptions:
            self.found_exceptions.append(name)

Make it take a node instead of name.

Then create a function that prints all the Exceptions in the format you expect.

Like:

def print_exceptions(self):
  for exception in self.found_exceptions:
    print(f"Found {exception.name} on line {exception.lineno}`)

I think something like that would work?

@NicolasMICAUX
Copy link
Author

Yes that's kind of what I've tried. But I've never been able to recover the file from which the line is from. So when analyzing multi file code, it would not work :(

@shadycuz
Copy link
Member

Do you need the path to the file or the name of it?

The path might not be that helpful as it's usually pretty long.

@NicolasMICAUX
Copy link
Author

Ideally the path when one want to find exceptions raised in a lot of submodules, for example i was using it to find errors raised by a complex dataloader.
But true that having the name of the file is better than nothing.

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

No branches or pull requests

2 participants