-
Notifications
You must be signed in to change notification settings - Fork 731
Make Formatter generic for the benefit of typeshed #2081
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
Conversation
I'm not very keen on merging this; it would be an isolated case of strange typing stuff without any benefit for us. If providing types via typeshed cannot handle such cases, it looks like it's buggy and should be fixed. |
Right, I understand. |
Providing types via typeshed is already a suboptimal solution and generally considered to be temporary. The design of that class in pygments makes it difficult to type correctly, and impossible to not introduce these discrepancies between stubs and code. Ideally, the typing information should be moved here and supported as part of the project itself. If that is an option and someone wants to start on it, I'm happy to help with code reviews. |
I cannot say I like how the "optional" typing is creeping in everywhere. If the other @pygments/maintainers are strongly in favor, I won't veto it, but I won't do the work. |
I'd only do typing if we do it a) properly and b) once a newer version of Python which makes it easier to express the information becomes our baseline, most likely 3.10. Till then I see limited value -- and it will leave the codebase in a "half done" state which I really don't look forward to. |
Sounds reasonable. If there are things missing in the typing module from newer versions, you can always just import them from typing_extensions in older python versions, in case you want to do it a bit earlier. |
I'm totally new as a committer here, but I concur with @birkenfeld and @Anteru. One point I'd like to raise is that, by nature, Pygments is a project attracting many different one-time contributors adding and updating lexers for their pet languages, who are fluent in that language and may not be fluent in Python. Judging from the concepts proposed in typing PEPs posted on python-dev, it seems to me that writing typed Python code still poses a number of challenges, and thus makes for a steeper learning curve for modifying Pygments code than I would prefer. |
strong disagree. making small contributions in an unfamiliar repo is dramatically easier to get right when you have type annotations and the IDE support they enable providing a guardrail |
The type stubs for Pygments in the typeshed have made
pygments.formatter.Formatter
generic in python/typeshed#6819 so that anoutfile
passed topygments.highlight()
must be opened in binary mode when the formatter was created with a givenencoding
, for example:This does however come with the disadvantage that subclassing
Formatter
no longer type checks withmypy --strict
:because mypy complains with
Missing type parameters for generic type "Formatter"
. The problem with that is that fixing this by specifying the type parameter would result in a runtime error because the realpygments.formatter.Formatter
is not generic and thus not subscriptable.Making the actual
Formatter
generic improves the situation for pygments users using static type checkers and should not change anything for the pygments users that don't.