Goose Types
Project description
Goose Types is a Python library for data types that support custom methods for type checking.
Goose types are classes with custom methods that define the behavior of isinstance() and issubclass() checks against themselves, along with some related annotations and error handling. In other words, a goose type is a frontend for Python’s metaclass hooks for custom instance and subclass checks.
Relation to duck typing
This is related to duck typing, and could even be interpreted as an implementation of it, but it is subtly different.
In contrast with a type in a nominal or structural type system, a duck type is defined by a partial structure or behavior that is checked at runtime. In this sense, a goose type is the same. However, duck typing is typically associated with one of two approaches that differ from goose typing.
One approach to duck typing is to apply EAFP: assume that the input implements the desired structure and behavior, and rely on error handling (supplementing as needed) to deal with non-compliant input. This is a fine approach for simple duck types and loose validation requirements, but for interfaces that require strict checking of complex type descriptions, the validation and error handling code can be cumbersome, littering a function and obscuring its essential functionality.
Another approach is to apply a form of nominal type checking at the level of attributes or methods, sometimes together with checking the multiplicity of arguments. By inspecting an object’s attributes, it is possible to check for a combination of interface conditions, such as attribute names, method names, and argument multiplicities, before commencing a destructive or expensive task.
Like the latter approach, goose types are more useful when it is desirable to perform complex validation up front. With Goose Types, complex type checking code to be extracted from a function body into the body of a goose type, leaving behind an invocation of isinstance() or issubclass(). In the sense that this is runtime checking of desirable interface behavior, this is like duck typing. However, unlike the conventional “walks”/”quacks” tests of duck typing, this can be used for checks that are exactly as extensive, specific, or generic as needed, without littering the code of functions that merely need to invoke a type check.
Changelog
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file GooseTypes-0.1.1.tar.gz
.
File metadata
- Download URL: GooseTypes-0.1.1.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9943b6dc245a55334bf702a994f0660d2842ecb3d1d711e60ab20ad2aa0f9bba |
|
MD5 | 7801a014abf7547392d46d6c8ecca500 |
|
BLAKE2b-256 | 610991f72299ae60d84f03db969e6bd09575fa3b6ff20d39cdbc63a07d6e3ac6 |
File details
Details for the file GooseTypes-0.1.1-py2.7.egg
.
File metadata
- Download URL: GooseTypes-0.1.1-py2.7.egg
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10750ae642b7187eb0c54d240bb435ed11ba91694c5bdea671f2d88996c9ef82 |
|
MD5 | 75930cf8005ba995caa6f08e83442047 |
|
BLAKE2b-256 | e76b91999126163c68f4ac9cb0fd6f704e63c2840e15c48714c60749a6d9437f |
File details
Details for the file GooseTypes-0.1.1-py2-none-any.whl
.
File metadata
- Download URL: GooseTypes-0.1.1-py2-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5733e0910ed9bd9e6292c0a4c4de12595c3479890a2a2d1453318320d0a74119 |
|
MD5 | c9d47b737575d3138a328119fc490ead |
|
BLAKE2b-256 | 4bb8fc57784797370345c045c42ee5eb0e7ed6b3f332904f3cc020e23538a3c3 |