Skip to main content

Custom type system for Python similar to TypeScript

Project description

TypePyX

TypePyX is a Python module similar to TypeScript, providing strong type checking, constant variables, and array/dictionary types.

Installation

pip install typepyx

Features

  • It supports
  • Constant Variables
  • Strong Type Checking
  • Array Types like int[], str[] or list
  • Dictionary Types like dict[str, int]
  • Arithmetic operations
  • Union Types support (int | str)

Usage

  1. Basics
  2. Data Types
  3. Basics around Arrays and Dictionary
  4. Operations
  5. Union Types

Basics

Creating the variable

from typepyx import setType
'''
Creates an variable with type string
'''
string = setType(value="Hello, World!", type="str")

print(string) # prints Hello, World!

Creating an Constant Variable

from typepyx import setType
'''
Creates an variable with type string and constant is True
'''
string = setType(value="Hello, World!", type="str", const=True)

print(string) # prints Hello, World!

Changing Value to Variables

from typepyx import setType

string = setType(value="Hello, World!", type="str")
string2 = setType(value="Hi", type="str", const=True)
# Changes the string variable value (but make sure its same type as the new value)
string.value = "Hello"
# The Constant Variable cannot be changed if you do change it will give you an error
string2.value = "Const" # Error!
print(string) # prints Hello

Data Types

TypePyX supports the following types:

  • int - integer numbers
  • float - decimal numbers
  • str - string values (e.g., "Hello")
  • bool - boolean values (e.g., True, False)
  • list - arrays (e.g., int[], str[]) (Special Type)
  • dict - dictionaries (e.g., dict[str, int]) (Special Type)
  • any - any type

Basics around Arrays and Dictionary

To create an Typed Array

from typepyx import setType
# To create an typed array you must include an normal type and add [] after you typed an data type you want

ls = setType(value=[1,2,3], type="int[]")
print(ls) # [1,2,3]

To create an Typed Dictionary

from typepyx import setType
'''
To create an typed dictionary
use this in setType type parameter dict[key_type, value_type]
key_type - the type of dictionary keys
value_type - the type of dictionary values
here is example of that down here
'''
dc = setType(value={"a": 1}, type="dict[str, int]")
print(dc)

Operations

Here is an example of operations

from typepyx import setType

a = setType(5, "int")
b = setType(3, "int")

print(a + b)  # 8
print(a - b)  # 2
print(a * b)  # 15
print(a / b)  # 1.6666666667

Arrays also support element-wise operations if both arrays have a same type and length

from typepyx import setType
arr1 = setType([1, 2, 3], "int[]")
arr2 = setType([4, 5, 6], "int[]")
print(arr1 + arr2)  # [5, 7, 9]

Union Types

Example of Union Types

import typepyx as tp
'''
To use Union Types use this in setType type parameters
type1 | type2 | ... 
type1 - type 1 to allow
type2 - type 2 to allow
... - more to allow
'''
# Variable can be either int or str
value = tp.setType(10, "int | str")
print(value)  # Output: 10

value.value = "Hello"
print(value)  # Output: Hello

value.value = 3.14  # ❌ Raises TypeError, because float is not allowed

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

typepyx-1.0.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

typepyx-1.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file typepyx-1.0.2.tar.gz.

File metadata

  • Download URL: typepyx-1.0.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typepyx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ca200acd4d3b32ca8231091dcc553ef203897e72fb786ea4e43207ac05b75581
MD5 f9060d48edd76920f11d878a1b3a05b6
BLAKE2b-256 d490d7414f88f6c23ec89416911f05f50f766d3a9ed49f09892f4d2a1dcedc2e

See more details on using hashes here.

File details

Details for the file typepyx-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: typepyx-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typepyx-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd00c242ac928a428a570716539fe0d71db2926fee240de10d5b936f0f7aab70
MD5 559d53ca18dc3ff780c780a9e1d03fa1
BLAKE2b-256 a2c0e9cda3d1fa5eaa2b9a0bf764b53e3b58bd3cfbe50f6757649af9eeb155ee

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