You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reconsider aspects of setup.cfg in packaging tutorial #871
While I appreciate the desire to have metadata declared statically, I'm a bit sceptical about the switch to setup.cfg in the packaging tutorial. The setup.cfg format is not backed by a standard and lacks support for lists and dictionaries. Non-scalar values are custom-parsed with an opaque grammar which is specific to setuptools - consider nested optional key = value, or some lists being comma-separated while others are split on new lines. In effect, this means that setup.cfg can only be parsed by setuptools - it is not a generic configuration format - and anybody who wants to make extensive use it, for e.g. metadata extraction, will depend on setuptools. I do not find that it's more readable than Python either. The structurelessness hinders readability.
Other than that, src-based trees are awkward to teach. The setuptools configuration is unintuitive. Several different options must be set at different locations in the file:
A newcomer would be justifiably confused that they have to repeat src; will almost certainly not understand the meaning of <empty space> = src; might overlook the fact that options.packages.find modifies the find: directive further up; and might not even understand the meaning of find:. We've also seen that they might confuse the value of package_dir items with the directory of their package if they try to adapt an existing project to the recommended format. I think that this latter change to the structure of the project should be reverted.
Reacted by 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко)
Well, this tutorial is specifically about setuptools, why would you expect it to be generic? Also, the declarative format is safer (no arbitrary code during the build time) and is recommended most of the time, unless there's a need to do some weird dynamic logic (which is rather rare). Besides, it is possible to combine setup.py with setup.cfg meaning that the static stuff is nice and declarative while it is still possible to have some Python code in setup.py.
This recommendation existed among PyPA and the wider community for years already. The change in the tutorial only documented what used to be tribal knowledge passed from maintainer to maintainer.
Well, this tutorial is specifically about setuptools, why would you expect it to be generic?
It probably wasn't clear that I was referring to the advantages listed when the adoption of setup.cfg was proposed here.
Also, the declarative format is safer ...
I'm not questioning whether static metadata are broadly better than dynamic metadata. What I'm wondering is whether teachingsetup.cfg and src trees is better than teaching setup.py to an audience unfamiliar with the intricacies of Python packaging in a format which is burdened by some fifteen-odd years of organic evolution and which wasn't originally intended to house package metadata. Arbitrary code execution doesn't really factor in here, and it's an issue that's better solved through the adoption of wheels.
I'm strongly in favor of setup.cfg. It's a better format for most projects - and even ones that need some dynamic code, it's better to keep the static information in setup.cfg. Would PEP 621 pyproject.toml be better? Yes, but it's not supported yet. Converting to pyproject.toml will happen years down the line. Setuptools needs to support it, as well as flake8 and mypy. It will be easier to convert if you are coming from a static format, though - you can even automate the conversion. You can't automate pretty much anything with setup.py. Also, running Python code just to define metadata, along with the weird "call setup() and this will turn into a runnable script" feature are old and unusual too, if you think about it.
Also, we didn't remove setup.py setup, it's just in a tab. If you are teaching it personally, you can pick.
There are also lots of problems with setup.py that I keep having to help fix in the wild. open().stuff() on one line is very common, and generally doesn't handle encoding issues correctly, and often breaks PyPy due to the non-standards reliance on garbage collection. Lots of things are simpler in setup.cfg, as well.
The issue with the format itself - well, you have to use it for quite a few other things normally, so currently you can't really skip setup.cfg completely. If you have to learn it someday, why not now? And while it's a weird format, it's baked into the standard library via cfg parser, so it's a standard of sorts.
src trees help avoid many common mistakes, including ones new users make, when they try to run their package from the main directory. It's too easy to run on a non-packaged version of the code from the local directory and hide the fact that you have an issue in packaging. Yes, more problems will arise, because you are forcing the packaging system to be used. It's not helpful to teach packaging then have users run from the local directory anyway!
Yes, students are struggling a little more, but that's because they are learning more, and more useful/correct stuff. I would love to stop seeing setup.py packages in the wild. I'd love to see more src packages. Etc.
Reacted by Matthew Feickert and 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко)
Reacted by 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) and Dane Hillard
Arbitrary code execution is not a problem for security, it's a problem for users doing things they should not do. I've seen hundreds of setup.py's, many of which were doing evil things that had much better solutions that would have been required if it was static. Building and manipulating paths, opening and messing with files, globbing, not opening files correctly (see above), etc. Add a single unicode character to a project's readme, try to run it with PyPy, or use an unusually mounted file system; you'll find a lot of setup.py projects blow up.
Neither flake8 nor mypy need to support pyproject.toml - both of these have their own stand-alone configuration files and it's not uncommon to see popular projects keep them separate. There might be the odd tool out there which only supports setup.cfg - I can't think of one off the top of my head. Either way, flake8 and mypy do not appear in the packaging tutorial.
Calling setup() and having it run as a script is weird and unusual but it's something people tend not to think about and will not be exposed to if they follow the tutorial.
I agree with the other points that you've made. While, yes, setup.cfg is less likely to lead users astray, the format and the way src trees are configured are hard to grok. If that's a price we're willing to pay to avoid some false negatives, that's fair enough. That's an angle I hadn't properly considered, that some errors are easier to mask when using setup.py.
Reacted by 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко)
Those formats are still cfg based. My point is that someone learning to make Python packages still needs to learn cfg files eventually, so why not now? Eventually, this might go away, but it's not gone yet.
What sort of packages do you want to see being produced? I would like to see setup.cfg packages with src/ layouts be the most common type of package added to PyPI, with (non-trivial) setup.py being reserved for places it's truly needed. We need to be teaching what we expect to be seeing.
We might be able to improve the explanations of how src and find work. Maybe a tabbed callout contrasting the two layouts and their settings?
Reacted by Matthew Feickert, 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) and Dane Hillard
I don't have much to add as I already agree with everything that @webknjaz and @henryiii have added and their points are clear and well reasoned, and with regards to
I think that this latter change to the structure of the project should be reverted.
I hard disagree. So I'll just ask some questions that haven't been addressed yet.
A newcomer would be justifiably confused that they have to repeat src; will almost certainly not understand the meaning of <empty space> = src; might overlook the fact that options.packages.find modifies the find: directive further up; and might not even understand the meaning of find:.
As @henryiii has already pointed out, we should be teaching people how to do things in the tutorials. @layday maybe you could open an Issue that expresses specifically how you think these isn't being conveyed clearly enough and give examples of how you think they should be? That would be truly helpful IMO and if the maintainers agree then it would be easy to translate that into a PR.
We've also seen that they might confuse the value of package_dir items with the directory of their package if they try to adapt an existing project to the recommended format.
Who is "we" in "we've" (sorry this probably seems comical to ask, but I really don't know given my limited contributions here)? Are you talking about specific experiences that you've had in giving tutorials and training sessions RE: packaging? If so, can you link to specific instances or even post tutorial survey data (which would be super awesome!)? I imagine that seeing information on what students find confusing could be truly helpful in improving these tutorials.
While, yes, setup.cfg is less likely to lead users astray, the format and the way src trees are configured are hard to grok.
As the format of setup.cfg isn't going to be changed, can you be more specific in your experience about what people find difficult to understand (or is it just the above bit that I quoted with all the ; in it)? If you have specific examples maybe they could be integrated into notes to the reader in the tutorials.
@layday I just wanted to add that especially for people coming from many other languages, it is quite common to have a config managing the build process rather than a seemingly arbitrary script.
Think about it: when we show the folks a script, it is like implicitly encouraging them to put any code they like there. But practically, it's only required for complex cases. Even if we put a big red warning saying "don't turn setup.py into a replacement of Makefile", I don't think it'd be helpful. People tend to break the rules. Because they can. Because "they know better". And while sometimes they do have a legitimate need to do so, it's unfair to present this "feature" to the beginners who are prone to misunderstanding what the best practices are because of the lack of experience.
I do understand where the problems with setup.cfg may come from (like the difference in configparser that Python 2 and Python 3 have, as in having to escape % as %% or the parsing will fail under py3 but not py2 which preserves both percent signs 🤯). But I believe that we need to identify these problems and add notes about them and it'll be a safer approach. The tutorial is supposed to show one non-branchy way of doing things leaving the exploration of the countless other options to the next steps that the learners are to discover in the future, separately.
Thanks all for your input. I don't really have much more to say here that I haven't already said. I don't think the poor ergonomics of setup.cfg can be dismissed, but I agree that there's good sense in using a DSL for build configuration. With the adoption of pypa/build, ideally, setuptools should not be recommended to beginners at all - I'd welcome a discussion centred around alternatives.
As an aside, I don't think thumbing down good-faith contributions sets the right tone.
I haven't had time to read more than the OP, so take this response with a grain of salt.
I do think teaching setup.cfg is preferable. From Setuptools' perspective, it's the preferred format to define a project, with setup.py being supported for compatibility and fallback.
Regarding the clumsy aspects, like requiring multiple config values for a "src layout", I agree, that's a wart. I'd like for the project to have some reasonable defaults, like:
if 'src' appears in the tree, assume a "src layout" and configure accordingly.
packages=find_namespace should be enabled by default (find all packages).
Activity
webknjaz commentedon Apr 3, 2021
Well, this tutorial is specifically about setuptools, why would you expect it to be generic? Also, the declarative format is safer (no arbitrary code during the build time) and is recommended most of the time, unless there's a need to do some weird dynamic logic (which is rather rare). Besides, it is possible to combine
setup.pywithsetup.cfgmeaning that the static stuff is nice and declarative while it is still possible to have some Python code insetup.py.This recommendation existed among PyPA and the wider community for years already. The change in the tutorial only documented what used to be tribal knowledge passed from maintainer to maintainer.
webknjaz commentedon Apr 3, 2021
cc @jaraco @pganssle any setuptools options on this?
webknjaz commentedon Apr 3, 2021
cc @henryiii @matthewfeickert you contributed to the related tutorials, any opinions?
webknjaz commentedon Apr 3, 2021
@layday re: src-layout
It was agreed on the PyPA Mini-Summit two years ago that it's the best practice that needs to be baked in the tutorials AFAIR.
layday commentedon Apr 3, 2021
It probably wasn't clear that I was referring to the advantages listed when the adoption of
setup.cfgwas proposed here.I'm not questioning whether static metadata are broadly better than dynamic metadata. What I'm wondering is whether teaching
setup.cfgandsrctrees is better than teachingsetup.pyto an audience unfamiliar with the intricacies of Python packaging in a format which is burdened by some fifteen-odd years of organic evolution and which wasn't originally intended to house package metadata. Arbitrary code execution doesn't really factor in here, and it's an issue that's better solved through the adoption of wheels.henryiii commentedon Apr 3, 2021
I'm strongly in favor of setup.cfg. It's a better format for most projects - and even ones that need some dynamic code, it's better to keep the static information in setup.cfg. Would PEP 621 pyproject.toml be better? Yes, but it's not supported yet. Converting to pyproject.toml will happen years down the line. Setuptools needs to support it, as well as flake8 and mypy. It will be easier to convert if you are coming from a static format, though - you can even automate the conversion. You can't automate pretty much anything with setup.py. Also, running Python code just to define metadata, along with the weird "call
setup()and this will turn into a runnable script" feature are old and unusual too, if you think about it.Also, we didn't remove setup.py setup, it's just in a tab. If you are teaching it personally, you can pick.
There are also lots of problems with setup.py that I keep having to help fix in the wild.
open().stuff()on one line is very common, and generally doesn't handle encoding issues correctly, and often breaks PyPy due to the non-standards reliance on garbage collection. Lots of things are simpler in setup.cfg, as well.The issue with the format itself - well, you have to use it for quite a few other things normally, so currently you can't really skip setup.cfg completely. If you have to learn it someday, why not now? And while it's a weird format, it's baked into the standard library via cfg parser, so it's a standard of sorts.
srctrees help avoid many common mistakes, including ones new users make, when they try to run their package from the main directory. It's too easy to run on a non-packaged version of the code from the local directory and hide the fact that you have an issue in packaging. Yes, more problems will arise, because you are forcing the packaging system to be used. It's not helpful to teach packaging then have users run from the local directory anyway!Yes, students are struggling a little more, but that's because they are learning more, and more useful/correct stuff. I would love to stop seeing setup.py packages in the wild. I'd love to see more src packages. Etc.
henryiii commentedon Apr 3, 2021
Arbitrary code execution is not a problem for security, it's a problem for users doing things they should not do. I've seen hundreds of setup.py's, many of which were doing evil things that had much better solutions that would have been required if it was static. Building and manipulating paths, opening and messing with files, globbing, not opening files correctly (see above), etc. Add a single unicode character to a project's readme, try to run it with PyPy, or use an unusually mounted file system; you'll find a lot of setup.py projects blow up.
layday commentedon Apr 3, 2021
Neither flake8 nor mypy need to support pyproject.toml - both of these have their own stand-alone configuration files and it's not uncommon to see popular projects keep them separate. There might be the odd tool out there which only supports
setup.cfg- I can't think of one off the top of my head. Either way, flake8 and mypy do not appear in the packaging tutorial.Calling
setup()and having it run as a script is weird and unusual but it's something people tend not to think about and will not be exposed to if they follow the tutorial.I agree with the other points that you've made. While, yes,
setup.cfgis less likely to lead users astray, the format and the waysrctrees are configured are hard to grok. If that's a price we're willing to pay to avoid some false negatives, that's fair enough. That's an angle I hadn't properly considered, that some errors are easier to mask when usingsetup.py.henryiii commentedon Apr 3, 2021
Those formats are still cfg based. My point is that someone learning to make Python packages still needs to learn cfg files eventually, so why not now? Eventually, this might go away, but it's not gone yet.
What sort of packages do you want to see being produced? I would like to see setup.cfg packages with src/ layouts be the most common type of package added to PyPI, with (non-trivial) setup.py being reserved for places it's truly needed. We need to be teaching what we expect to be seeing.
We might be able to improve the explanations of how src and find work. Maybe a tabbed callout contrasting the two layouts and their settings?
matthewfeickert commentedon Apr 3, 2021
I don't have much to add as I already agree with everything that @webknjaz and @henryiii have added and their points are clear and well reasoned, and with regards to
I hard disagree. So I'll just ask some questions that haven't been addressed yet.
As @henryiii has already pointed out, we should be teaching people how to do things in the tutorials. @layday maybe you could open an Issue that expresses specifically how you think these isn't being conveyed clearly enough and give examples of how you think they should be? That would be truly helpful IMO and if the maintainers agree then it would be easy to translate that into a PR.
Who is "we" in "we've" (sorry this probably seems comical to ask, but I really don't know given my limited contributions here)? Are you talking about specific experiences that you've had in giving tutorials and training sessions RE: packaging? If so, can you link to specific instances or even post tutorial survey data (which would be super awesome!)? I imagine that seeing information on what students find confusing could be truly helpful in improving these tutorials.
As the format of
setup.cfgisn't going to be changed, can you be more specific in your experience about what people find difficult to understand (or is it just the above bit that I quoted with all the;in it)? If you have specific examples maybe they could be integrated into notes to the reader in the tutorials.webknjaz commentedon Apr 7, 2021
@layday I just wanted to add that especially for people coming from many other languages, it is quite common to have a config managing the build process rather than a seemingly arbitrary script.
Think about it: when we show the folks a script, it is like implicitly encouraging them to put any code they like there. But practically, it's only required for complex cases. Even if we put a big red warning saying "don't turn
setup.pyinto a replacement ofMakefile", I don't think it'd be helpful. People tend to break the rules. Because they can. Because "they know better". And while sometimes they do have a legitimate need to do so, it's unfair to present this "feature" to the beginners who are prone to misunderstanding what the best practices are because of the lack of experience.I do understand where the problems with
setup.cfgmay come from (like the difference inconfigparserthat Python 2 and Python 3 have, as in having to escape%as%%or the parsing will fail under py3 but not py2 which preserves both percent signs 🤯). But I believe that we need to identify these problems and add notes about them and it'll be a safer approach. The tutorial is supposed to show one non-branchy way of doing things leaving the exploration of the countless other options to the next steps that the learners are to discover in the future, separately.layday commentedon Jun 19, 2021
Thanks all for your input. I don't really have much more to say here that I haven't already said. I don't think the poor ergonomics of
setup.cfgcan be dismissed, but I agree that there's good sense in using a DSL for build configuration. With the adoption ofpypa/build, ideally, setuptools should not be recommended to beginners at all - I'd welcome a discussion centred around alternatives.As an aside, I don't think thumbing down good-faith contributions sets the right tone.
jaraco commentedon Jun 19, 2021
I haven't had time to read more than the OP, so take this response with a grain of salt.
I do think teaching
setup.cfgis preferable. From Setuptools' perspective, it's the preferred format to define a project, withsetup.pybeing supported for compatibility and fallback.Regarding the clumsy aspects, like requiring multiple config values for a "src layout", I agree, that's a wart. I'd like for the project to have some reasonable defaults, like:
packages=find_namespaceshould be enabled by default (find all packages).