Python 3.7.0b2 in Fedora

On February 28th, the second beta of Python 3.7 has been released, making several new features and lots of fixes available for everyone to test. We're happy to announce that the pre-release is available in Fedora Rawhide, and it's coming soon to Fedora 27 as well. To install it, simply do:

sudo dnf install python37

(On Fedora 27, you might still get the Beta 1 if you do this right now. Use --enablerepo updates-testing if you're impatient.)

Then, run python3.7 to test things out! You can also create a virtual environment using python3.7, or add py37 to your project's tox.ini and start testing on the freshest Python available. You won't find any extra libraries or software for Python 3.7 packaged in Fedora, but the whole ecosystem is available through virtualenv and pip.

If you spot any bugs, please report them in Fedora Bugzilla or directly upstream. And if you have any questions, ask the Python SIG at python-devel@lists.fedoraproject.org or #fedora-python on freenode!

So, what's new in 3.7? There's an exhaustive list you can go through, but let me introduce my two favorite features: data classes and importlib.resources. Coincidentally, both are library additions, and you can use them in older Pythons as well – they're available as third-party libraries from PyPI.

Data classes

How many times have you written out self.attribute = attribute in your __init__ method? For most Python devs, the answer is – a lot. Combined with __repr__ and support for comparisons, there's a lot of boilerplate involved in creating a class that “just” holds a bunch of data. The excellent attrs project solves many of these issues, and now a carefully selected subset of the ideas in attrs is making its way into the standard library. Look:

from dataclasses import dataclass

@dataclass
class Point:
    x: float
    y: float
    z: float = 0.0

>>> p = Point(1.5, 2.5)
>>> print(p)
Point(x=1.5, y=2.5, z=0.0)

Note that the types are just documentation – they aren't actually checked at runtime, though they will work with static checkers like mypy. Data classes are documented in PEP 557 for now. If the API feels too limited, remember the small scope is intentional! You can always switch to attrs if you need more features.

importlib.resources

Many Python libraries modules need data files like templates or images. So far, there have been two main ways to access these: craft a filesystem path based on a module's __file__, or use pkg_resources from setuptools, which is more complex, but supports, for example, loading modules and their data directly from zip files.

Setuptools is a monolith spanning lots of various areas of functionality, and it's getting harder and harder to maintain. There have been efforts to take the ideas that work and implement them in a more reasonable place. For example, easy_install was replaced by pip, and namespace packages have been integrated into the standard import machinery. Data files are the next to go: Python 3's importlib has long had hooks for loading them, and all that was missing is a friendly API. That's importlib.resources. You can start using it right now – importlib_resources is available on PyPI for Python versions as low as 2.7, and has probably the friendliest documentation of the 3.7 changes.

Other additions

There have been a number of other exciting additions, which you can start using as soon as you switch to Python 3.7.

Notable changes

Of course, not every change is a straightforward addition. Sometimes, existing behavior needs to be adjusted. Here are the most visible improvements:

To ensure your code works in Python 3.7, check out the Porting section in the What's New document. You'll find that most of the breaking changes are rare edge cases – but, unfortunately, one of them might bite you.

If something does go very wrong, now is the best time to let the developers know – all changes and new features can still be reverted.

Fedora changes

Since you're still reading, let me throw in a few highlights around Fedora packaging for Python!

Fedora 28 has a new feature for packagers: an automatic dependency generator for RPMs. If you say %?python_enable_dependency_generator in a spec file, Requires: on Python packages will be automatically generated from egg/wheel metadata. We hope this will simplify lots of packages, and prevent mistakes. (By the way, another simplification many packages can make is removing support for Python 2, which has just two years to live.)

Back to 3.7: we're planning to make it the the default Python version in Fedora 29.

That's it for now, thanks for reading! And again, if you'd like to discuss anything related to Python in Fedora, please join us on the Python SIG mailing list, or point your IRC client to #python-fedora on Freenode.