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.3a0.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.3a0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file typepyx-1.0.3a0.tar.gz.

File metadata

  • Download URL: typepyx-1.0.3a0.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.3a0.tar.gz
Algorithm Hash digest
SHA256 8e69bbc72519665062e97837593cb647e6706bd00f73c10388f5e6ac4c0371d4
MD5 c2b6390ed352a9c1808729f29ebb53e8
BLAKE2b-256 0a97da5a28990ff67ae7fbb51ba1092e50081ff49a8337cb367594c46ecd4af4

See more details on using hashes here.

File details

Details for the file typepyx-1.0.3a0-py3-none-any.whl.

File metadata

  • Download URL: typepyx-1.0.3a0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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.3a0-py3-none-any.whl
Algorithm Hash digest
SHA256 7edd41d52781a031e35c04c3da67f629c0b2f600182a76a17468c3155e441714
MD5 719dd853a8584d6b524e430df79daf51
BLAKE2b-256 bc768e1d1b4909e97a5584749ee95a1a511f5da8f4829b9c35b9ea4860d14a83

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