Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
Python equivalent for typedef

What is the python way to define a (non-class) type like:

typedef Dict[Union[int, str], Set[str]] RecordType
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This would simply do it?

from typing import Dict, Union, Set

RecordType = Dict[Union[int, str], Set[str]]


def my_func(rec: RecordType):
    pass


my_func({1: {'2'}})
my_func({1: {2}})

This code will generate a warning from your IDE on the second call to my_func, but not on the first. As @sahasrara62 indicated, more here https://docs.python.org/3/library/stdtypes.html#types-genericalias

over 4 years ago · Santiago Trujillo Denunciar

0

In case users look for a distinct nominal typedef:

from typing import Dict, Union, Set, NewType

RecordType = Dict[Union[int, str], Set[str]]
DistinctRecordType = NewType("DistinctRecordType", Dict[Union[int, str], Set[str]])

def foo(rec: RecordType):
    pass

def bar(rec: DistinctRecordType):
    pass

foo({1: {"2"}})
bar(DistinctRecordType({1: {"2"}}))
bar({1: {"2"}}) # <--- this will cause a type error

This snippet demonstrates that only explicit casting will do.

$ mypy main.py
main.py:14: error: Argument 1 to "bar" has incompatible type "Dict[int, Set[str]]"; expected "DistinctRecordType"
Found 1 error in 1 file (checked 1 source file)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda