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

3.11 StrEnum members are reduced to str in some cases #3997

Closed
layday opened this issue Oct 1, 2022 · 8 comments
Closed

3.11 StrEnum members are reduced to str in some cases #3997

layday opened this issue Oct 1, 2022 · 8 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@layday
Copy link

layday commented Oct 1, 2022

Describe the bug
This is the most peculiar set of preconditions, but if all of the following are true:

  • If click is installed and
  • if a function within a package is decorated with click.group or click.command and
  • if the function is imported in the package's __main__ module and
  • if the function is called

... then StrEnum members are reduced down to simple strs.

I was not able to repro this if I renamed __main__, or if I used a locally-defined decorator, or if I did not call the function, or if I constructed a custom StrEnum by subclassing str and Enum.

To Reproduce

  1. (Optionally?) install Python 3.11.0rc2
  2. Clone https://github.com/layday/pyright-311-strenum-repro
  3. Ensure click is installed
  4. Type check the pkg with npx -y pyright --pythonversion 3.11

The output is:

No configuration file found.
No pyproject.toml file found.
stubPath /[...]/typings is not a valid directory.
Assuming Python platform Darwin
Searching for source files
Auto-excluding /[...]/venv
Found 2 source files
pyright 1.1.273
/[...]/pkg/module1.py
  /[...]/pkg/module1.py:17:13 - information: Type of "Foo.bar" is "str"
  /[...]/pkg/module1.py:18:13 - information: Type of "some_foos" is "set[str]"
0 errors, 0 warnings, 2 informations

Expected behavior
That Foo.bar would be of type Foo and some_foos of type set[Foo].

VS Code extension or command-line
CLI, 1.1.273

erictraut pushed a commit that referenced this issue Oct 1, 2022
… a `StrEnum` subclass under certain circumstances. This was caused by a circular dependency in `ReprEnum` caused by pyright's validation of `__init_subclass__` as part of class type evaluation. This change moves this validation step from the type evaluator to the checker, eliminating the potential for circular dependencies and improving performance. This addresses #3997.
@erictraut
Copy link
Collaborator

Thanks for the bug report and clear repro. This will be addressed in the next release.

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Oct 1, 2022
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.274, which I just published. It will also be included in a future release of pylance.

@layday
Copy link
Author

layday commented Jan 19, 2023

@erictraut I believe this has resurfaced in v1.1.290:

No configuration file found.
No pyproject.toml file found.
stubPath /[...]/typings is not a valid directory.
Assuming Python platform Darwin
Searching for source files
Found 2 source files
pyright 1.1.290
/[...]/pkg/module1.py
  /[...]/pkg/module1.py:18:13 - information: Type of "Foo.bar" is "str"
  /[...]/pkg/module1.py:19:13 - information: Type of "some_foos" is "set[str]"
0 errors, 0 warnings, 2 informations
Completed in 1.078sec

@erictraut
Copy link
Collaborator

Same repro steps as above? If the repro steps are different, please open a new issue. If they're the same, I'll reopen this one.

@layday
Copy link
Author

layday commented Jan 19, 2023

They are the same.

@erictraut erictraut reopened this Jan 19, 2023
@erictraut erictraut removed the addressed in next version Issue is fixed and will appear in next published version label Jan 19, 2023
erictraut pushed a commit that referenced this issue Jan 20, 2023
…types in some rare circumstances. The problem is due to a circular dependency in the `enum.pyi` type stub involving the `StrEnum`, `ReprEnum` and `EnumMeta` classes. The solution is to detect this case and defer evaluation to handle the circular dependency. This addresses #3997.
@erictraut
Copy link
Collaborator

Yes, this was a regression introduced in another recent bug fix. Normally our tests prevent such regressions, but I haven't been able to repro this bug under conditions of our test suite. It's extremely dependent on the order in which evaluation occurs, and any small change in the ordering makes the problem go away.

The underlying problem is caused by a circular dependency in the way that StrEnum is declared. It derives from ReprEnum which derives from Enum which is based on the metaclass EnumMeta which has a __new__ method that accepts an argument of type FlagBoundary which itself derives from StrEnum.

I've made our circular dependency detection logic more robust to accommodate this case. This will be included in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 20, 2023
@layday
Copy link
Author

layday commented Jan 20, 2023

Thank you for the detailed explanation.

@erictraut
Copy link
Collaborator

This is included in pyright 1.1.291, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants