Ability to use Callable type alias when annotating functions
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 1.8k
- Forks
- 302
- Merge moyen
- 23 h
- PR mergées (30 j)
- 8
Description
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!
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par lire les discussions liées dans les issues 1641 et 2087 ainsi que dans la pull request 3291, puis comparez les approches proposées avec l’alias Callable et @declared_type à la solution de contournement basée sur Protocol présentée ici. Le travail sera considéré comme terminé lorsqu’un comportement clairement choisi et accepté pour l’annotation des définitions de fonctions, ainsi que le travail d’implémentation et de validation correspondant, auront été définis ; cette issue ne nomme aucun fichier ni test.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- devtools
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100