-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
WIP: Add Typing Support (PEP 561 - issue #447) #493
base: master
Are you sure you want to change the base?
Conversation
Pull Request Test Coverage Report for Build 6308941580730368
💛 - Coveralls |
This PR attempts to bring changes based on #493 for tox.ini and .cirrus.yml (this is a long standing tradition of using mostly of the same configuration provided by the templates to PyScaffold's own repository and CI). I have also noticed that, most of the times in Debian docker containers `apt-get install` will fail unless the program is already installed, because a `apt-get update` is required. In the Debian-based Python containers used in Cirrus, git seem to be pre-installed.
Thanks for the suggestions! Sorry for the late reply and I also only had a quick look at the changes. Right now got I lot of work on my plate as colleagues are on vacation. I must say that it really looks a bit complicated and adds more complexity in the PyScaffold codebase for a feature I am not 100% convinced of. Option 3, you are mentioning, actually sounds like the best for me. This is for several reasons. Most Python programmers I work with are adopting types in their code directly. And with PyScaffold we are addressing clean code affine and thus more advanced developers anyway, not beginners that just start scripting. So for our target group seeing some types shouldn't be a problem and I strongly believe typed Python code will become the default in production-grade code. Since PyScaffold helps you to develop production-grade packages, why not have a template example that is typed by default? Option 3 would be simple, with no need for an extension and extra complexity in the code-base. What do you think? |
76b5b80
to
423b773
Compare
Thank you very much for the review @FlorianWilhelm, I implemented a few changes:
The remaining complexity is due to:
I have not added the ability to add a typecheck task to It is up for discussion if we don't want to support b, c or d, but I have the feeling that we don't have much option for a (unless we opt for not having a |
This PR attempts to bring changes based on #493 for tox.ini and .cirrus.yml (this is a long standing tradition of using mostly of the same configuration provided by the templates to PyScaffold's own repository and CI). I have also noticed that, most of the times in Debian docker containers `apt-get install` will fail unless the program is already installed, because a `apt-get update` is required. In the Debian-based Python containers used in Cirrus, git seem to be pre-installed.
4c2abfd
to
a332715
Compare
@abravalheri thank you for all the work. Also haven't had time to check it before. In Cirrus this might work as well: In Tox this is available as well: So the idea would be make typing default and only have the switch change the little variable / add the typed file. |
Thank you very much for the suggestions @CarliJoy! In the future, what I was thinking is:
Of course, those things need to be debated and mainly I need some time to develop them, but I think it could be cool. |
Thanks @CarliJoy, I like your solution. So typing by default, a switch for the checks in PyScaffold and in total not much more complexity. Sounds really good :-) |
@FlorianWilhelm, do you prefer always performing the typecheck in Cirrus and just failing silently, or use a environment var and (Both are basically changing a switch) |
Will
Sounds cool. So this would even automize then the switch @CarliJoy mentions. So |
Failing silently doesn't sound so good. |
My idea here is to simply define the API in terms of strings. If the pyscaffold/src/pyscaffold/extensions/typed.py Lines 102 to 118 in a332715
(and what we are doing in the
I was thinking more in terms of But yes, I agree that changing Python code is complicated... Nevertheless we are already doing that for
Yes that is the idea, but it is for the future 😝, when I have sometime... |
Sorry, I messed up the idea between For
It would be more or less a extraction of what we use in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good and sorry for the long wait :-)
Thank you very much for the review No problems, I will leave this change marinating for a while until I have the time to proper plan for a release 😝 Maybe rethink somethings... |
The changes here try to address #447, but they would be more of a RFC than a PR...
I tried to add the most complete implementation that would support the use case described, however there might be room for simplification (currently it is very complex).
One of the things that I noticed is that this implementation highlights problems related to #336, and exposes an abstraction leak if we move in that direction: the project structure abstraction works very well for updating files, but the mental model is not easily applicable to updating parts of files or composing them incrementally with different extensions.
In the code provided we can see this abstraction leak by comparing the
add_typing_support
andupdate_existing_config
actions: they are very similar. The fact that we need 2 different actions that are alternating most of times is also a symptom. Themodify_file
is basically doing the same asstructure.modify_contents
but instead of operating in the dict representation, it operates directly on the file system.I followed the example listed in the PEP561 which places a
src/{package}/py.typed
file. In principle thenamespace
extension have the machinery to automatically convert the struct.Simplifications we could do, and reasons why I haven't implemented that way (yet):
We could add a
typecheck
task totox.ini
by default, since running tox is opt-in.pyscaffoldext-django
and https://github.com/typeddjango/django-stubs). The way I implemented it was by exposing atypecheck_deps
option that can be augmented when extensions are activated. The logic to add these deps totox.ini
by default would be very similar to the one implemented and therefore I don't see many simplifications by making it a default behaviour.We could add the mypy config to
setup.cfg
by default.django-stubs
previously mentioned) require mypy to have some plugins configured, and therefore we would need to have some level of manipulation ofsetup.cfg
anyway.We could add type hints to
skeleton.py
by default.I have chosen to not do so because some Python beginners coming from other languages might feel overwhelmed by a type system.
Things that I dislike in the PR implementation:
retype
being an optional dependency that is required to run--type
. The alternative would be either makingTyped
an external extension or makingretype
a default dependency...retype
do not pass theisort
pre-commit.Things that I like in the current implementation:
structure.modify_contents
seems to be a nice addition to the API.