Giter Site home page Giter Site logo

stasharrofi / pytyped Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 2.0 119 KB

A set of Python packages to enable as much type-driven development as possible in Python, i.e., start with defining your type and let repetitive mundane tasks be automated.

License: MIT License

Python 98.78% Dockerfile 0.72% Shell 0.50%

pytyped's People

Contributors

louis-jaris avatar stasharrofi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pytyped's Issues

Bug in generic extractions

The following code enters an infinite loop in the extractor:

from dataclasses import dataclass
from typing import List, Generic, TypeVar

from pytyped.json.encoder import AutoJsonEncoder

T = TypeVar("T")


@dataclass
class A(Generic[T]):
    t: T


@dataclass
class B:
    x: int


@dataclass
class C(Generic[T]):
    l: List[A[T]]


a = AutoJsonEncoder()
a.extract(C[B])  # enters an infinite loop here

Class hierarchies as sum types

Currently, the only union types supported are Union[T1, T2, ...., Tn] that cannot be named. For example, it is not possible to distinguish Union[T1, Union[T2, T3]] from Union[Union[T1, T2], T3]. However, in most real-world scenarios, sum types are named and these names come in handy when tagging serialized sum types.

A prevalent example of named sum-types are class hierarchies.

For example, when decoding JSON, an unnamed union can be as unrestricted as a priority-based decoder but a named union has some structure to it such as being a JSON object and using the name of the super class as a field inside the JSON to show the type that is being decoded. In the absence of named unions, such a field would either have to have a very generic name (such as tag) or it would have to be manually generated.

So, three main asks:

  1. Separate the concept of named unions from unnamed unions.
  2. Implement class hierarchy auto extraction in extractor.
  3. Implement both named and unnamed JSON decoding/encoding.

One additional ask:
Decoding/encoding JSON for named unions should allow both the following:

{
  "SuperClassName": "SubClassName",
  ...  // sub-class fields
}

and

{
  "type": "SubClassName",
  "value": sub_class_encoding
}

Improve README

There are positions marked as WIP. It can also benefit from more details and examples. In particular sum types, product types, generics, etc can all be defined more clearly.

Missing required dependency `python-dateutil`

How to reproduce:

cd /tmp

# Creating a new venv
python3 -m venv foo
source foo/bin/activate
pip install --upgrade pip

# Install the lib
pip install pytyped

# Try to simply import it
python -c 'from pytyped.json.decoder import JsonDecoder, AutoJsonDecoder'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/tmp/foo/lib/python3.8/site-packages/pytyped/json/decoder.py", line 8, in <module>
    from dateutil import parser
ModuleNotFoundError: No module named 'dateutil'

How to fix:

pip install python-dateutil

python -c 'from pytyped.json.decoder import JsonDecoder, AutoJsonDecoder'
# Works fine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.