Skip to main content

tools to make typing more consistent in older versions of python3, and access generic variables at runtime

Project description

RT-Generic

RT-Generic is a typed python module to make accessing the actual runtime types from a generic module more straight forward. The process is as follows:

  • Declare your generic class (shown "old style and new style"), adding RTGeneric as a base class
# import stuff you need from types, typing, and/or typing_extensions

from rt_generic import * # deliberately not much
T = TypeVar('T')
U = TypeVar('U')
class MyClass(Generic[T,U], RTGeneric):

or

# import stuff you need from types, typing, and/or typing_extensions

from rt_generic import * # deliberately not much
class MyClass[T,U](RTGeneric):
  • In the body of your class, if you need to behave differently based on the type of one of the generic types, you can access it's type in a class method
	@classmethod
	def cls_method(cls, argv) :
		T_type = cls.tv2type(MyClass,T) # type: ignore[misc] # mypy doesn't want T here
		if cls.generic_true(MyClass,U) # type: ignore[misc] # Was U set to TrueT (Literal type defined in rt-generic)
		...

or an instance method:

	def method(self, argv) :
		T_type = self.tv2type(MyClass,T) # type: ignore[misc] # mypy doesn't want T here
		if self.generic_true(MyClass,U) # type: ignore[misc] # Was U set to TrueT (Literal type defined in rt-generic)
		...
  • Finally, RTGeneric relies on there being an explicit declartion of the non-generic particlar class(es)
class B_Class(MyClass[float,FalseT]):
	pass
	
class C_Class(MyClass[list[int],TrueT]):
	pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rt_generic-0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rt_generic-0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file rt_generic-0.1.tar.gz.

File metadata

  • Download URL: rt_generic-0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for rt_generic-0.1.tar.gz
Algorithm Hash digest
SHA256 a5a843fbcbb98539e61f1652bf233470cac038e72ad293d2505bdb3f612c1004
MD5 7661eed5d56d5fa8867221844761a5e9
BLAKE2b-256 ef68cfb37b3e8543cc8911f4018409c4a98d8fa0b0e61f4b091c1da8959e8415

See more details on using hashes here.

File details

Details for the file rt_generic-0.1-py3-none-any.whl.

File metadata

  • Download URL: rt_generic-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for rt_generic-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a80a3b9bce548b63f9a0add5e78a31f3d0b63dd51857bab53b221a408408df3a
MD5 257ecc5313fdb7210584ec51134a69cc
BLAKE2b-256 c893bdd146d23b1ed4a51f48929ef74f013c12b4c516fcf077986443f2bcd3db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page