We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

EOX: Adding static type hints to fiona

00:00

Formal Metadata

Title
EOX: Adding static type hints to fiona
Title of Series
Number of Parts
266
Author
License
CC Attribution 3.0 Germany:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
Static type hints according to PEP 484 (and its extensions) have been a part of Python since version 3.5, which came out in 2015. Research from 2021 shows that 3 out of 4 Python developers already use optional type hinting at least sometimes in their projects. Time is ripe for static type hints to enter the FOSS4G Python world! This talk will give an overview on the current status of the effort to add type hints to fiona. Furthermore it will briefly discuss considerations and the reasoning behind design decisions taken up until then. Contributions to the effort are very much welcome – just take part in the discussion on GitHub.
Type theoryStatic random-access memory
Service (economics)Static random-access memoryType theorySteady state (chemistry)Group actionConfiguration spaceError messageRun time (program lifecycle phase)Mathematical analysisComputer fileKey (cryptography)Information overloadCommunications protocolType theoryCodeKey (cryptography)Error messageStatic random-access memoryPolygonStress (mechanics)Point (geometry)MereologyRevision controlComputer fileObject (grammar)Social classElectronic data processingMathematical analysisExpert systemShared memoryInterface (computing)Visualization (computer graphics)GeometrySatelliteInformation overloadSoftware testingRight angleBitRun time (program lifecycle phase)Row (database)Electronic mailing listArrow of timeLevel (video gaming)Line (geometry)Machine visionSuite (music)TrailNumberTupleCommunications protocolComputer animation
Transcript: English(auto-generated)
Hi again, welcome to my talk adding static type hints to Fiona. My name is Stefan. I work for UX. We deal with satellite data processing and satellite data visualization.
The company is based in Vienna, Austria. We use Fiona in data import pipelines, data export pipelines. Static type annotations for Python were introduced in Python version 3.5, which was released eight years ago. The type annotations gained widespread traction in the Python ecosystem, but not so much yet
in the geospatial part of the Python ecosystem. So the idea is to find errors in your Python code before actually running the code. So by static analysis and comparing the types that you use in the code, you are
notified about errors that might occur when you run the code later. A year ago, discussion started in the Fiona issue track about adding type annotations to the Fiona code base. The vision was to add type hints as sidecar files, so-called type stub files, to the
stable version, the current stable version 1.9 of Fiona, and then later inline these type annotations into the code base in version 2.0. I would like to note that this does not change any runtime behavior of the code,
so this is purely for static analysis before running the code itself. The approach of adding type annotations to the Fiona code base was first to use a tool that auto-generates static types from the code base that is already there. There are some tools that do static analysis, but we found one tool called
market type by Instagram, which does runtime analysis of the code. So it runs the code, it compares all the types, all the objects that are used in the Python code, and then generates type annotation files from that. And it was much more reliable than the static analysis that other tools do.
We use the pytest suite for that, so market type runs the tests of Fiona and then finds out the object types that are used in the code. We configured mypy to actually use these type annotation files, and mypy found
about 100 errors that could not be inferred by market type itself. So we went through the whole list manually and fixed those errors and added types here and there that market type could not infer. And the goal was to add stricter types to the public API of Fiona,
for example, the write records method. And this is where we're currently a bit stuck, because there's one problem in the write records, and the public API accepts objects that are like a geoJSON or that convert to the geo interface, done the methods of Python objects.
We tried two approaches, one was to use the type Dict class, but a geoJSON allows any keys inside of the object. So there has to be a type key, which could be a point or could be polygon, and then there has to be a coordinates key for a point.
The coordinates value is always a tuple of two numbers, for example. But the type Dict class in Python does not allow extra keys, so we cannot add any keys, for example, that a geoJSON might have. The second approach was to use a protocol class
with overloads for the get item done the methods. So we tried to mimic a dictionary in Python. We found that it cannot overload literal types. So I cannot make an overload for the type point
and then have another value for the type polygon. We could not make a fix for that. So this is where we're currently stuck, and if you have any ideas for other approaches, I would be very interested in that, so you can approach me after the session or just write in the GitHub issue tracker of Fiona and share your ideas, please.
Thank you for listening.