hugapi / hug

Embrace the APIs of the future. Hug aims to make developing APIs as simple as possible, but no simpler.
MIT License
6.86k stars 388 forks source link

typing: error when using 'Union' / 'Optional'. #507

Open feluxe opened 7 years ago

feluxe commented 7 years ago

Hi there!

I just started using hug a couple of days ago and I like it a lot. It feels very intuitive and the docs are nice to read. Kudos! :)

I get an error when I use Union or Optional from the typing module like this:

from typing import Optional

@hug.get('/images/{filename}', output=hug.output_format.file)
def get_image(filename: str, w: Optional[str] = None):
    """
    ...

Parameter w is optional, so it should be None by default or a str if provided, rigth? If I call the API like this:

http://127.0.0.1:8000/images/foo.jpg?w=1400

I get this return:

{"errors": {"w": "Cannot instantiate typing.Union"}}

Am I doing it wrong or is this a bug?

BrandonHoffman commented 7 years ago

Hug currently does not support much of python new typing module but you should be able to specify an optional url parameter by just setting the default value like you did above. Just use w: str = None instead of w: optional[str] = None.

This weekend I am hoping to make changes that will allow hug to start using at least some of the new typing module features.

feluxe commented 7 years ago

Thanks for your reply!

This weekend I am hoping to make changes that will allow hug to start using at least some of the new typing module features.

This is cool! I really love the new typing module, would be great if hug supports it.

leophys commented 6 years ago

Hi @BrandonHoffman, hug is just great! I just wanted to check out if there are any news here?

May I help somehow?

Greetings

GhostofGoes commented 5 years ago

This is still an issue as of Hug 2.4.1

Spenhouet commented 4 years ago

Hug still does not seem to support typing.

My static type checker warns me about:

import hug
       ^^^
       Stub file not found for "hug"
@hug.get(urls=
^^^^^^^^
Untyped function decorator obscures type of function; ignoring decorator