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

fix(commands): resolve unstringified annotation before caching #1120

Merged
merged 4 commits into from
Oct 19, 2023

Conversation

shiftinv
Copy link
Member

Summary

utils.evaluate_annotation uses a (per-signature) cache to speed up resolving forwardrefs.
This would previously cache the eval'd result before continuing to resolve it, meaning subsequent calls with the same stringified annotation return the (incomplete) cached result.

from __future__ import annotations

@bot.command()
async def cachetest(ctx: commands.Context, a: Union['int', 'str'], b: Union['int', 'str']) -> None:
    ...

# In both cases, we resolve the outer forwardref ("Union[...]" -> Union[...]),
# but only resolve the inner ones in the first call:
#
# 'ctx': <Parameter "ctx: disnake.ext.commands.context.Context">
# 'a': <Parameter "a: Union[int, str]">
# 'b': <Parameter "b: Union[ForwardRef('int'), ForwardRef('str')]">

The same thing happens with other unique evaluate_annotation behaviors, like turning UnionTypes into Unions.

The correct behavior here is to cache after we've done all the remaining steps.

Checklist

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running pdm lint
    • I have type-checked the code by running pdm pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@shiftinv shiftinv merged commit c2a8dde into master Oct 19, 2023
26 checks passed
@shiftinv shiftinv deleted the fix/annotation-forwardref-cache branch October 19, 2023 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant