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. Variable with Types
  2. Data Types
  3. Basics around Arrays and Dictionary
  4. Operations
  5. Union Types

Variable with Types

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

Union Types work on arrays as well

import typepyx as tp

# Variable can be either int or str array
value = tp.setType([10,"90"], "int | str[]")
print(value) # Prints [10, "90"]
value.append(7)
value.append("80")
print(value) # Prints [10, "90", 7, "80"]
value.append(3.14) # Error because the parameter type is float not int or str

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.4.tar.gz (3.7 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.4-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typepyx-1.0.4.tar.gz
  • Upload date:
  • Size: 3.7 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.4.tar.gz
Algorithm Hash digest
SHA256 720900b04e23e78a7c8bcad8a843cec738faca97324eea48dcd7ceb8f5ee225f
MD5 19bf94d971f0f07ca59444c4c35eb578
BLAKE2b-256 cccc27ed7b2e06b57aa470d6145ed1e1560d0a2360c50f412ddd99b0dbf538fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typepyx-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ac1f9548a96b588f732997e3ff62af73285bf78056c4b6747742442ff66abfb9
MD5 f41316bfb63975606e5a2f79f7c2c9cb
BLAKE2b-256 131a45751dc4305d827a7ffeda73ff605f3abac6a033022ec36e1ecee72a2809

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