Ability to use Callable type alias when annotating functions
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
Direzione di ricerca
Inizia leggendo le discussioni collegate nelle issue 1641 e 2087 e nella pull request 3291, quindi confronta gli approcci proposti dell’alias Callable e di @declared_type con il workaround basato su Protocol mostrato qui. Il lavoro sarà considerato completato quando sarà stato definito un comportamento chiaramente scelto e accettato per annotare le definizioni delle funzioni, insieme al relativo lavoro di implementazione e validazione; questa issue non indica file o test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hello. I would like to revive the discussion about Callable type aliases. The original discussion is here, and the related discussion about @declared_type is here and here.
The original thread contains a great example of the problem and a proposed solution:
from typing import Callable
import math
ActivationFunction = Callable[[float], float]
sigmoid: ActivationFunction
def sigmoid(x):
return 1 / (1 + math.exp(-x))
relu: ActivationFunction
def relu(x):
return max(0, x)
Another prosed solution is to use the @declared_type decorator:
@declared_type(ActivationFunction)
def sigmoid(x):
return 1 / (1 + math.exp(-x))
@declared_type(ActivationFunction)
def relu(x):
return max(0, x)
Currently, this can be done with Protocols, but it's too verbose:
from typing import Protocol
import math
class ActivationFunction(Protocol):
def __call__(self, x: float) -> float:
...
class Sigmoid(ActivationFunction):
def __call__(self, x: float) -> float:
return 1 / (1 + math.exp(-x))
class Relu(ActivationFunction):
def __call__(self, x: float) -> float:
return max(0, x)
sigmoid = Sigmoid()
relu = Relu()
print(sigmoid(0.5))
print(relu(0.5))
If the function is a single expression, it can be done very nicely with lambdas:
from typing import Callable
import math
ActivationFunction = Callable[[float], float]
sigmoid: ActivationFunction = lambda x: 1 / (1 + math.exp(-x))
relu: ActivationFunction = lambda x: max(0, x)
print(sigmoid(0.5))
print(relu(0.5))
Here's the equivalent FSharp code, with the only difference being that FSharp supports multiline lambdas:
type ActivationFunction = float -> float
let sigmoid: ActivationFunction = fun x -> 1.0 / (1.0 + exp(-x))
let relu: ActivationFunction = fun x -> max 0.0 x
printfn "%f" (sigmoid 0.5)
printfn "%f" (relu 0.5)
I'm asking about this feature because I'm reading a book called Domain Modeling Made Functional by Scott Wlaschin. In this book, the author recommends modeling the domain with types, including function type aliases, and focusing on the implementation details later. Here's an example from Chapter 9:
I'd like to try a similar approach in Python. Has there been any further discussion about this topic, or is there a workaround I'm unaware of? Thank you!
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/typing
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
topic: conformance tests topic: typing spec
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
Tutte le issue di python/typing
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
learningequality/ricecooker#747 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
run-llama/llama_index#23199 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
KhronosGroup/glTF-Blender-IO#2769 ·