OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Type hint for a function that returns only a specific set of values

  • Thread starter Thread starter Copperfield
  • Start date Start date
C

Copperfield

Guest
I have a function that can only return a, b or c, all of them are of type T. I want to include this fact in the signature because of the special meaning they carry in the context of the function. How do I do that?

Currently, I use this

Code:
def fun(...) -> "a or b or c":
    #briefly explain the meaning of a, b and c in its docstring

Is that the correct one?

I know that I can do this

Code:
def fun(...) -> T:
    # briefly explain the meaning of a, b and c in its docstring

but as I said, I want to express in the signature that the function only returns those specific values.
<p>I have a function that can only return <code>a</code>, <code>b</code> or <code>c</code>, all of them are of type <code>T</code>. I want to include this fact in the signature because of the special meaning they carry in the context of the function. How do I do that?</p>
<p>Currently, I use this</p>
<pre><code>def fun(...) -> "a or b or c":
#briefly explain the meaning of a, b and c in its docstring
</code></pre>
<p>Is that the correct one?</p>
<p>I know that I can do this</p>
<pre><code>def fun(...) -> T:
# briefly explain the meaning of a, b and c in its docstring
</code></pre>
<p>but as I said, I want to express in the signature that the function only returns those specific values.</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top