Skip to content

Conversation

@colin-nolan
Copy link
Contributor

@colin-nolan colin-nolan commented Sep 8, 2025

This PR allows standard tracebacks to be enabled using the environment variable: TYPER_STANDARD_TRACEBACK.

The work is a follow up to the discussion with @svlandeg: #1284.

The existing environment variable _TYPER_STANDARD_TRACEBACK is retained but referred to as "deprecated" because:

  • It is non-standard for an advertised env to start with an underscore.
  • Environment variables that start with an underscore have been observed as causing issues in some execution environments (e.g. with AWS Lambda).

Manual testing with:

import typer

def main():
    raise Exception("Hello World")

if __name__ == "__main__":
    typer.run(main)

Produces expected results:

$ python will-fail.py 
╭───────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────╮
│ /Users/cnolan/repositories/typer/will-fail.py:5 in main                                                             │
│                                                                                                                     │
│   2 import typer                                                                                                    │
│   3                                                                                                                 │
│   4 def main():                                                                                                     │
│ ❱ 5 │   raise Exception("Hello World")                                                                              │
│   6                                                                                                                 │
│   7 if __name__ == "__main__":                                                                                      │
│   8 │   typer.run(main)                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Exception: Hello World
$ TYPER_STANDARD_TRACEBACK=1 python will-fail.py 
Traceback (most recent call last):
  File "/Users/cnolan/repositories/typer/will-fail.py", line 8, in <module>
    typer.run(main)
    ~~~~~~~~~^^^^^^
  File "/Users/cnolan/repositories/typer/typer/main.py", line 1073, in run
    app()
    ~~~^^
  File "/Users/cnolan/repositories/typer/typer/main.py", line 332, in __call__
    raise e
  File "/Users/cnolan/repositories/typer/typer/main.py", line 315, in __call__
    return get_command(self)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 1442, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/typer/core.py", line 700, in main
    return _main(
        self,
    ...<6 lines>...
        **extra,
    )
  File "/Users/cnolan/repositories/typer/typer/core.py", line 193, in _main
    rv = self.invoke(ctx)
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 1226, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 794, in invoke
    return callback(*args, **kwargs)
  File "/Users/cnolan/repositories/typer/typer/main.py", line 692, in wrapper
    return callback(**use_params)
  File "/Users/cnolan/repositories/typer/will-fail.py", line 5, in main
    raise Exception("Hello World")
Exception: Hello World
$ _TYPER_STANDARD_TRACEBACK=1 python will-fail.py 
Traceback (most recent call last):
  File "/Users/cnolan/repositories/typer/will-fail.py", line 8, in <module>
    typer.run(main)
    ~~~~~~~~~^^^^^^
  File "/Users/cnolan/repositories/typer/typer/main.py", line 1073, in run
    app()
    ~~~^^
  File "/Users/cnolan/repositories/typer/typer/main.py", line 332, in __call__
    raise e
  File "/Users/cnolan/repositories/typer/typer/main.py", line 315, in __call__
    return get_command(self)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 1442, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/typer/core.py", line 700, in main
    return _main(
        self,
    ...<6 lines>...
        **extra,
    )
  File "/Users/cnolan/repositories/typer/typer/core.py", line 193, in _main
    rv = self.invoke(ctx)
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 1226, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/cnolan/repositories/typer/.venv/lib/python3.13/site-packages/click/core.py", line 794, in invoke
    return callback(*args, **kwargs)
  File "/Users/cnolan/repositories/typer/typer/main.py", line 692, in wrapper
    return callback(**use_params)
  File "/Users/cnolan/repositories/typer/will-fail.py", line 5, in main
    raise Exception("Hello World")
Exception: Hello World

@github-actions

This comment was marked as outdated.

@svlandeg svlandeg added the feature New feature, enhancement or request label Sep 9, 2025
@svlandeg svlandeg changed the title Adds support for standard tracebacks via the env TYPER_STANDARD_TRACEBACK ✨ Add support for standard tracebacks via the env TYPER_STANDARD_TRACEBACK Sep 9, 2025
@svlandeg svlandeg self-assigned this Sep 9, 2025
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

As discussed, this PR adds the option to set an env var TYPER_STANDARD_TRACEBACK while still supporting the old var _TYPER_STANDARD_TRACEBACK too. We need to support both because we don't want to break existing workflows, but that does complicate the code slightly. I think that's an acceptable trade-off to ensure this variable can be properly set in AWS Lambda etc.

@svlandeg
Copy link
Member

svlandeg commented Sep 9, 2025

I will leave this PR for a final review by Tiangolo 🙏

@svlandeg svlandeg removed their assignment Sep 9, 2025
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Makes sense, thank you! 🚀

@tiangolo
Copy link
Member

It seems that I can't push to your branch GenPAxCo:feature/non-underscore-traceback-env to update with the changes in master, which would probably solve the failing tests. Maybe you can update it to "allow maintainers to push to the branch", or you can update it with master yourself, I think that should do it. 🤓

@colin-nolan colin-nolan force-pushed the feature/non-underscore-traceback-env branch from b77ece1 to 0a41362 Compare September 23, 2025 22:55
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg removed the waiting label Sep 24, 2025
@colin-nolan
Copy link
Contributor Author

@tiangolo I've updated and tests are now passing.

@svlandeg
Copy link
Member

svlandeg commented Oct 3, 2025

Thanks @colin-nolan! I've moved this back to Tiangolo's internal review queue 😉

@colin-nolan
Copy link
Contributor Author

Waiting on @tiangolo

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Nov 25, 2025
@github-actions

This comment was marked as resolved.

@svlandeg
Copy link
Member

@colin-nolan: can you please turn on the option that maintainers can edit this PR?

Keeps support fo `_TYPER_STANDARD_TRACEBACK` but notes as deprecated
because:
- It is non-standard for an advertised env to start with an underscore.
- Environment variables that start with an underscore have been observed as
  causing issues in some execution environments (e.g. with AWS Lambda).

See: fastapi#1284
@colin-nolan colin-nolan force-pushed the feature/non-underscore-traceback-env branch from 0a41362 to a75d349 Compare November 25, 2025 11:12
@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Nov 25, 2025
@github-actions
Copy link
Contributor

@colin-nolan
Copy link
Contributor Author

colin-nolan commented Nov 25, 2025

@svlandeg I've resolved the merge conflict.

I've tried to enable maintainers PR edits but, for some reason, the option to do this doesn't exist(?) (https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)
Screenshot 2025-11-25 at 11-20-53 ✨ Add support for standard tracebacks via the env `TYPER_STANDARD_TRACEBACK` by colin-nolan · Pull Request #1299 · fastapi_typer

Edit: On reading the docs from GH again, I note (emphasis mine):

Pull request authors can give these permissions when they initially create a pull request from a fork in a personal account or after they create the pull request.

The fork is from a company account. Its ambiguous as to whether permission can be added post creation but that could well the be issue.

@svlandeg
Copy link
Member

Ok, thanks @colin-nolan! As Tiangolo had already approved earlier, and nothing changed since then other than merging in master, I'm going to go ahead and merge this in. Thanks again!

@svlandeg svlandeg merged commit de159dc into fastapi:master Nov 25, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature, enhancement or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants