contains-io / typet Goto Github PK
View Code? Open in Web Editor NEWTypes that make coding in Python quick and safe.
License: MIT License
Types that make coding in Python quick and safe.
License: MIT License
When using a default value, the type should be inferred from that value instead of requiring it to be specified.
When a default value is supplied, Object knows not to require the value but does not seem to be setting the value in new instances.
Add pypy to tox tests.
mypy
reports validation objects as invalid type aliases.
Additionally, if a validation type is used as a class annotation without being aliased, mypy
ends analysis with an invalid syntax error.
See python/mypy#4285.
The project needs to be thoroughly documented with examples and tutorials.
When using @singleton on a class that does not inherit from Object (no other metaclasses defined) throws an exception.
Traceback (most recent call last):
File "<input>", line 1, in <module>
@singleton
File "/home/dangle/Projects/contains.io/containment/.tox/py36/lib/python3.6/site-packages/typet/meta.py", line 58, in _inner
class _Meta(base, _Meta): # pylint: disable=function-redefined
TypeError: Cannot create a consistent method resolution
order (MRO) for bases type, Singleton
It should be possible to run this test:
from typingplus import is_instance
from typet import Bounded
assert is_instance(5, Bounded[int, 0:10])
I believe this causes an issue when using validation types with StrictObject.
This can be done by setting __instancecheck__
and __subclasscheck__
in BoundedMeta._BoundedSubclass
.
from typet import Object
class Base(Object):
x: int = 5
class Sub(Base):
pass
Sub().x
In [4]: _RcliConfig.call()
TypeError Traceback (most recent call last)
in ()
----> 1 _RcliConfig.call()
~/rcli/venv/lib/python3.6/site-packages/typet/meta.py in call(cls, args, **kwargs)
78 else:
79 try:
---> 80 cls.instance.singleton(args, **kwargs) # type: ignore
81 except AttributeError:
82 pass
TypeError: 'NoneType' object is not callable
The code in question is here:
https://github.com/contains-io/typet/blob/master/typet/meta.py#L64
Currently, using Generics with Object is a pain. Initial support for casting and type validation exist, but creating the class itself is awkward as it requires creating a metaclass.
from typet import Object
from typing import Generic, GenericMeta, TypeVar
T = TypeVar('T')
class Meta(type(Object), GenericMeta): ...
class MyObject(Object, Generic[T], meta=Meta):
value: T
typet/__init__.py
is becoming unwieldy. It should be broken into multiple packages and imported into the package with wildcards.
Initial proposed packages:
Create a SingletonObject
by creating a private metaclass that inherits from Singleton
and _ObjectMeta
and a metaclass for a StrictSingletonObject
that inherits from Singleton
and _StrictObjectMeta
.
The proposed use case would be a global settings object:
from typet import SingletonObject, File
class _Configuration(SingletonObject):
config: File = '~/.my_config'
settings = _Configuration()
The path validation objects, File, Dir, Path, and ExistingPath should instantiate pathlib objects instead of strings. They should also accept pathlib objects.
A check to import pathlib2 will need to be added to setup.py.
Perhaps we could say "checking"?
typet
needs to supply a rich collection of types by default. This issue will track ideas for the initial types.
Because the annotations are read during the metaclass before the class is created, it may be necessary to add support for reading the type hint comments in the metaclass.
If I make a proper typet Object, I can still assign to its attributes at run time, subsequent comparison or hash operations on the resulting instance will now return unexpected results. I believe the solution is to add logic to set_attr to prevent non-Object, or StrictObject assignment.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.