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

Pygments: Add return types for format/highlight. #6819

Merged
merged 32 commits into from
Jan 8, 2022
Merged

Pygments: Add return types for format/highlight. #6819

merged 32 commits into from
Jan 8, 2022

Conversation

Dreamsorcerer
Copy link
Contributor

No description provided.

@Akuli
Copy link
Collaborator

Akuli commented Jan 4, 2022

These functions can return str or bytes, and correspondingly accept a str or bytes file, depending on the formatter. Maybe we should make the formatters Generic, so that Formatter[str] would be a formatter that outputs strings, for example.

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Jan 4, 2022

Yes, you're right. I was hoping to avoid needing to type the formatters as well.

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Jan 4, 2022

I'm not sure how to actually type that. You'd want Formatter[str] if instantiated as Formatter(), and Formatter[bytes] if instantiated with Formatter(encoding="some_str") or Formatter(outencoding="some_str"). But, a Generic expects the type to be the same as a given parameter...

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Jan 4, 2022

Maybe it's possible to cheat a little. Maybe something with overloads like this? I'm sure this is wrong though:

@overload
def __init__(self, encoding: None = ..., __type: _T = "str"): ...
@overload
def __init__(self, encoding: str, __type: _T = b"bytes"): ...

@Akuli
Copy link
Collaborator

Akuli commented Jan 4, 2022

You are looking for self types:

@overload
def __init__(self: Formatter[str], encoding: None = ...) -> None: ...
@overload
def __init__(self: Formatter[bytes], encoding: str) -> None: ...

@Dreamsorcerer
Copy link
Contributor Author

Yep, didn't think about doing that on the __init__.

@Dreamsorcerer Dreamsorcerer changed the title Add return types for format/highlight. Pygments: Add return types for format/highlight. Jan 5, 2022
stubs/Pygments/pygments/formatter.pyi Outdated Show resolved Hide resolved
stubs/Pygments/pygments/__init__.pyi Outdated Show resolved Hide resolved
stubs/Pygments/pygments/__init__.pyi Outdated Show resolved Hide resolved
Comment on lines 13 to 16
@overload
def highlight(code, lexer, formatter, outfile: SupportsWrite[Any]) -> None: ...
@overload
def highlight(code, lexer, formatter, outfile: None = ...) -> str: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably annotate the formatter argument here too.

@Dreamsorcerer
Copy link
Contributor Author

OK, I think we're good now.

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

Thanks!

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 this pull request may close these issues.

3 participants