4 ms·
Does anyone else get bothered by how this seemingly results in code that won't compile? Instead of this: @ai() def describe(animals: list[str]) -> str: """
by babyshake 3y ago
Does anyone else get bothered by how this seemingly results in code that won't compile?
Instead of this:
@ai()
def describe(animals: list[str]) -> str:
"""
Given a list of animals, use one word that'd describe them all.
"""
it would seem a lot more intuitive to do this:
def describe(animals: list[str]) -> str:
return ai("""Given a list of animals, use one word that'd describe them all.""", animals)
- edunteman 3y agoYeah the pyright doesn't like the annotated return type not being honored by the empty stub function. I wonder if there's a way to trick it. For your suggestion, the decorator would still be required to overload the function execution with the remote call, otherwise you'd just be calling the function body, but we have considered special wrapper return types to help play better with pyright (and also give programmatic access to debug details of the call), but that'd add bloat to the package and subtract from the more native python feel we're aiming for.
- dragonwriter 3y ago> Yeah the pyright doesn't like the annotated return type not being honored by the empty stub function. I wonder if there's a way to trick it. Python has an existing convention for this (so its not a "trick"), the use of the special value Ellipsis (literal: ...) https://mypy.readthedocs.io/en/stable/stubs.html https://mypy.readthedocs.io/en/stable/stubs.html
- deleted 3y ago[deleted]
- edunteman 3y agoBeautiful, this does the trick!! Thanks for the tip. @ai() def stub() -> int: """docstring""" ... # (use ... instead of "pass" in the function body)
- deleted 3y ago[deleted]
- dragonwriter 3y agoWouldn't the correct way be with the use of ellipsis, as is used in type stubs? @ai() def describe(animals: list[str]) -> str: """ Given a list of animals, use one word that'd describe them all. """ ...
- pedrovhb 3y agoTechnically a function body needs at least one statement. A docstring is just an expression statement (a string), so a function definition with just a docstring is synctatically valid Python. I've seen people say multiline string literals are Python's version of multiline comments, but that's really just convention; it's a noop expression statement. Same as doing def foo(): 4 Which is also an expression statement as a function body, and also does nothing. Contrast to actually using a comment as a function body; comments aren't statements (nor expressions, so they can't be used as an expression statement): def foo(): # this doesn't work > IndentationError: expected an indented block after function definition on line 1 Of course, this doesn't really matter at all, and I get that it feels strange. I've just been thinking about grammars and syntax lately, and it's been interesting to now have the vocabulary and mental model to understand these unintuitive things :)
- thekombustor 3y agoI typically use "pass" for this exact case of having a stub function body (typically to be implemented later)