Coder Social home page Coder Social logo

Comments (4)

epsy avatar epsy commented on June 27, 2024

Hello!

Currently there is rudimentary support for using typing.Annotated in 5.0.0. It is not in the documentation, but there is an example that uses it: https://github.com/epsy/clize/blob/v5.0.0/examples/typed_cli.py

You have to wrap parts of the annotation meant for Clize with Clize[], for example your example would read:

def echo(*text:Parameter.REQUIRED,
         prefix: Annotated[str, Clize['p']] ='', 
         suffix: Annotated[str, Clize['s']] = '', 
         reverse: Annotated[bool, Clize['r']] = False, 
         repeat: Annotated[int, Clize['n']] = 1,
):
    ...

There's room to make this better, but this is a start. Let me know if you have feedback or ideas.

from clize.

richieadler avatar richieadler commented on June 27, 2024

Is Clize needed? Can't just be a Literal?

from clize.

epsy avatar epsy commented on June 27, 2024

It is kind of needed for me to require it, yes.

See PEP 593 which introduces typings.Annotated, which says:

Ultimately, the responsibility of how to interpret the annotations (if at all) is the responsibility of the tool or library encountering the Annotated type. A tool or library encountering an Annotated type can scan through the annotations to determine if they are of interest (e.g., using isinstance()).

and then later:

Namespaces are not needed for annotations since the class used by the annotations acts as a namespace.

So it seems like the PEP expects library authors to distinguish different annotations by their type, so unfortunately str would be too ambiguous.

I hear you on this being very verbose. The PEP has a suggestion on the subject:

T = TypeVar('T')
Const = Annotated[T, my_annotations.CONST]

class C:
    def const_method(self: Const[List[int]]) -> int:

Perhaps in the future it could be applied like:

ValueType = TypeVar("ValueType")
ClizeAnnotationsT = TypeVar("ClizeAnnotationsT")
TypeAndClize = typings.Annotated[T, ClizeAnnotationsT]

def main(prefix: TypeAndClize[str, "p"]):
    ...

Though I think Clize may have to be updated to understand TypeVar/ClizeAnnotationsT

from clize.

richieadler avatar richieadler commented on June 27, 2024

Though I think Clize may have to be updated to understand TypeVar/ClizeAnnotationsT

Agreed. And we may need to say something like

def main(a: str, b: Annotated[int, Parameter.REQUIRED]):
   ...

I don't know how to handle this case without Annotated...

from clize.

Related Issues (20)

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.