Skip to content

Big type hinting and modernization upgrade #147

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

Merged
merged 2 commits into from
Jun 2, 2025

Conversation

edward-jazzhands
Copy link
Contributor

Hello! So, a bit of explaining is in order. I'm the creator of Rich-Pyfiglet and Textual-Pyfiglet, which are implementations of Pyfiglet for Rich and Textual respectively. Rich-Pyfiglet is a fork of Pyfiglet (you can see in the forks list) and then Textual-Pyfiglet uses this fork.

So, I've been working with Pyfiglet for a while. Really big fan. I had noticed in this post on March 24 that you said you were open to PRs for type hints. So thought I would like to tackle this challenge. My two pyfiglet packages pass Pyright and MyPy in strict mode so I set out to try to apply that onto the Pyfiglet package itself.

This doesn't completely add type hints to everything but it does tackle the vast majority of them, basically everything I could figure out easily without needing to dig too deep into the internals. Important to note that there were absolutely no changes to run-time behavior in any way. Aside from a couple standard lib imports (annotations, typing.Any, typing.Optional), every single modification is just adding type hints, except one:
There is also a place in the code where the two uses of the property() function were replaced with the @Property decorator. This has been in python since the 2.x days so I thought this is a reasonable modification. I suppose it technically counts as a refactor, but hardly ;p. Aside from that, there was no refactoring done.

I have ran the unit tests and confirmed that everything still works perfectly:

pyfiglet/test.py

OK = 2238, FAIL = 0

pytest -vv

============================= test session starts =============================
platform linux -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /home/devuser/workspace/vscode-projects/rich-pyfiglet/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/devuser/workspace/vscode-projects/rich-pyfiglet
configfile: pyproject.toml
collected 3 items

pyfiglet/tests/test_cli.py::test_strip PASSED                           [ 33%]
pyfiglet/tests/test_cli.py::test_strip_strange_font PASSED              [ 66%]
pyfiglet/tests/test_cli.py::test_normalize PASSED                       [100%]

============================== 3 passed in 0.35s ==============================

For some further evidence that that my changes are good, I'd like to present two files as well that are attached to this post: pyright_old.txt and pyright_new.txt.
pyright_old.txt
pyright_new.txt

These results show running Pyright on strict mode. In pyright_old.txt, this shows running Pyright on the original version without any modifications. At the bottom of the file you can see:

414 errors, 97 warnings, 0 notes

But if you peek inside pyright_new.txt, you will see this:

75 errors, 91 warnings, 0 notes

That's about an 82% reduction in Pyright warnings. All the public API points are covered, and I also added a py.typed file. So this should now be fine to import and use in other programs without needing any # type: ignore comments.

For the record, I also added basedpyright (https://docs.basedpyright.com/) in the dev-requirements.txt, which is an open-source fork of Pyright. I personally prefer it for this kind of work over MyPy because the strict mode will flag every single thing that does not have a type hint, whereas MyPy does not do this and there's no simple way to make it do it that I'm aware of. Adding basedpyright of course required a config file so there's also a pyrightconfig.json file. This will allow any of you that wants to to use it and see the results for yourself.

But, please do look over my work and make sure I didn't make any mistakes. I have not had a second set of eyes overview this. If there's any modifications you want or you'd like me to reduce the scope for a first PR, that's totally fine, just let me know! Hope you like it.

@edward-jazzhands edward-jazzhands changed the title Huge type hinting and modernization upgrade Big type hinting and modernization upgrade May 22, 2025
@pwaller
Copy link
Owner

pwaller commented Jun 2, 2025

This looks good to me, thanks for contributing. I've just let the tests run and intend to merge when it's passing.

@pwaller pwaller merged commit da42ac4 into pwaller:main Jun 2, 2025
5 checks passed
@pwaller pwaller mentioned this pull request Jun 2, 2025
@pwaller
Copy link
Owner

pwaller commented Jun 2, 2025

Released in https://github.com/pwaller/pyfiglet/releases/tag/v1.0.3.

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.

2 participants