Skip to content

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

Merged
Akuli merged 32 commits into
python:masterfrom
Dreamsorcerer:patch-19
Jan 8, 2022
Merged

Akuli merged 32 commits into
python:masterfrom
Dreamsorcerer:patch-19

Conversation

@Dreamsorcerer

Copy link
Copy Markdown
Contributor

No description provided.

@Akuli

Akuli commented Jan 4, 2022

Copy link
Copy Markdown
Collaborator

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

Dreamsorcerer commented Jan 4, 2022 •

Copy link
Copy Markdown
Contributor Author

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

@Dreamsorcerer

Dreamsorcerer commented Jan 4, 2022 •

Copy link
Copy Markdown
Contributor Author

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

Dreamsorcerer commented Jan 4, 2022 •

Copy link
Copy Markdown
Contributor Author

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

Akuli commented Jan 4, 2022

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Comment thread stubs/Pygments/pygments/formatter.pyi Outdated
Comment thread stubs/Pygments/pygments/__init__.pyi Outdated
Comment thread stubs/Pygments/pygments/__init__.pyi Outdated
Comment thread stubs/Pygments/pygments/__init__.pyi Outdated
Comment thread stubs/Pygments/pygments/__init__.pyi Outdated
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
Copy Markdown
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
Copy Markdown
Contributor Author

OK, I think we're good now.

@Akuli Akuli left a comment

Copy link
Copy Markdown
Collaborator

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