My Framework Said Any: How a Pyrefly Error Led Me to Fix Typing in SwiftPY
I build SwiftPY , a Python web framework with an async Active Record ORM. Last week I opened one of my own repositories in VS Code and saw this: class DocRepository : model = Doc async def get_doc ( self , doc_id : int ) -> Doc : doc = await self . model . where ( "is_active" , True ) . with_ ( "group" ) . where ( "id" , doc_id ) . first ( ) if not doc : raise NotFoundError ( "Doc not found" )…
I built SwiftPY, an async Active Record ORM-based Python web framework. While working on a repository, I encountered an error stating that the framework returned Any instead of the expected "Doc" type. This error occurred despite the code running correctly and passing tests. The issue stemmed from the type chain having gaps, with certain method calls lacking return annotations, causing the checker to assign Unknown to the subsequent results.
SwiftPY addressed this in version 0.3.8 by carrying the concrete model type through the whole query chain, from ModelQueryBuilder[T] to SingleRelationshipQueryBuilder[T]. This provided developers with a seamless typing experience without the need for explicit annotations.
A secondary issue arose from a global setting in Pyrefly that allowed missing imports to pass silently, leading to the framework returning Unknown even when the code was valid. This occurred frequently in Docker-based workflows where the interpreter lacked the required framework. The fix in version 0.3.9 added per-package overrides to handle the specific libraries with missing type information, resulting in clear error messages when the interpreter lacked the necessary framework, such as "[missing-import] Could not find import of `swiftpy`".
The implementation of these fixes in SwiftPY alleviated the previously encountered issues, ensuring that type errors were now accurately reported and easily identifiable. This improvement not only benefited the developer experience but also integrated a robust CI process to catch such bugs early in the development cycle.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to