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

Avoid erroring from tracing #424

Open
astrojuanlu opened this issue Jul 20, 2023 · 4 comments
Open

Avoid erroring from tracing #424

astrojuanlu opened this issue Jul 20, 2023 · 4 comments

Comments

@astrojuanlu
Copy link

A user reported a weird condition in which pluggy tracing was making their code fail after we enabled tracing because one of the local objects didn't have a working __repr__ (a scikit-learn object from a very old version). Details: kedro-org/kedro#2630

We gave the user a workaround and also it's reasonable to expect that objects have a working __repr__, but it would be nice if there was a generous try ... except around the tracing messages so that errors there don't bubble up. Essentially the same that happens with logging:

>>> try:
...   logging.warning("Will fail: %d", "a")
...   logging.warning("But the show must go on")
... except:
...   logging.error("Oh no")
... else:
...   logging.warning("Everything is fine")
... 
--- Logging error ---
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 1110, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 953, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 687, in format
    record.message = record.getMessage()
                     ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 377, in getMessage
    msg = msg % self.args
          ~~~~^~~~~~~~~~~
TypeError: %d format: a real number is required, not str
Call stack:
  File "<stdin>", line 2, in <module>
Message: 'Will fail: %d'
Arguments: ('a',)
WARNING:root:But the show must go on
WARNING:root:Everything is fine
@RonnyPfannschmidt
Copy link
Member

Let's steal the safe repr from pytest

@astrojuanlu
Copy link
Author

@RonnyPfannschmidt You mean saferepr and SafeRepr, right?

https://github.com/pytest-dev/pytest/blob/73d754bd74ae8ddf86eb90cda29545cef495e927/src/_pytest/_io/saferepr.py#L102-L115

Is this open for PRs? Any guidance on where the code should go is much appreciated.

@RonnyPfannschmidt
Copy link
Member

Absolutely

@RonnyPfannschmidt
Copy link
Member

For now it can just go to the private tracing module

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