8000 Incorrect types for `keyword.kwlist` and `keyword.softkwlist` · Issue #9679 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Incorrect types for keyword.kwlist and keyword.softkwlist #9679

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

Closed
Eclips4 opened this issue Feb 4, 2023 · 5 comments · Fixed by #9680
Closed

Incorrect types for keyword.kwlist and keyword.softkwlist #9679

Eclips4 opened this issue Feb 4, 2023 · 5 comments · Fixed by #9680

Comments

@Eclips4
Copy link
Member
Eclips4 commented Feb 4, 2023

For some reason, kwlist and softkwlist typed as Sequence[str] instead of list[str]
Corresponding to Lib/keyword.py, it's pure lists.

@Eclips4 Eclips4 changed the title Incorrect types for keywords.kwlist and keywords.softkwlist Incorrect types for keyword.kwlist and keyword.softkwlist Feb 4, 2023
@TeamSpen210
Copy link
Contributor

This is probably intentional - these attributes are supposed to be constants, and really shouldn't be modified. Annotating them as Sequence will make the type checker flag any modification attempts. Could do with having a comment to mention that, and also making them Final[Sequence[str]].

@Eclips4
Copy link
Member Author
Eclips4 commented Feb 5, 2023

Thank you!
I think, it's good idea to making them Final[list[str]], instead of Final[Sequence[str]].
Why?
This test raises an Error:
Expected type 'list', got 'Sequence[str]' instead
Because of unittest.TestCase.assertListEqual, which accepts two lists.
And according to this test, changing value of kwlist breaks nothing.
What you think about this?

@TeamSpen210
Copy link
Contributor

Python's test suite shouldn't be an example of valid type-checkable code. In that case perhaps the test should be loosened to assertSequenceEqual(), making the fact that it's a list an implementation detail. Or treat the test as confirming the internal details match also. In general test suites aren't really good for type checking, since they may look at internals, do dynamic things like monkeypatching and deliberately run invalid code to confirm it errors. In CPython's case, it's also ensuring that you don't get fatal errors even for the most absurd code. For the second example, it has a comment saying that they're testing an implementation detail to ensure it doesn't change, in case someone is accidentally relying on it.

@Eclips4
Copy link
Member Author
Eclips4 commented Feb 5, 2023

Okay. I will change it to Final[Sequence[str]]. Thx for advices!

@AlexWaygood
Copy link
Member

I agree with @TeamSpen210 — these are documented as "sequences" rather than "lists": https://docs.python.org/3/library/keyword.html#keyword.kwlist

Trying to alter them at runtime would likely indicate buggy assumptions in a Python program. Better to keep them typed as if they're immutable.

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

Successfully merging a pull request may close this issue.

3 participants
0