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.0.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.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typepyx-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 f07ea29309360c9c69e9daad6eb5e146a0295d82ccecc799cb038e6d4a4f23a2
MD5 22d7d8e4619be0d79687a0b41e74d0d6
BLAKE2b-256 02902c4a789260c93783a4dbd211a12aaa03cc3492e93ba55ff5a50aad99b1c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typepyx-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c96b2d3e86b477fcc82c34efce9d344ab7c10ffd335fa841bb3ecaaae3470bcb
MD5 2454065bfc8953d489adffc74343e810
BLAKE2b-256 935a97bbce6cd2f1d91873797ec51084ecebef5e1a4885de1ef89cc44b29a691

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