Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
¿Cómo insinuar una lista homogénea de instancias de la misma subclase?
class A: pass class A1(A): pass class A2(A): pass def help_here(s: WHAT_SHOULD_I_TYPE_HERE ): # How to hint this function that accepts list, # where all elements should be instances of same subclass of A. # Example 1: [A1(), A1(), A1()] - good # all components all elements are same class, # and it is subclass of A # Example 2: [A2(), A2(), A2(), A2()] - valid. Same reason. # Example 3: [A1(), A1(), A2()] - invalid, not same classes. # Example 4: [1, 2] - invalid. Same class, but not subclass of A. if isinstance(s[0], A1): # If I use Union[List[A1], List[A2]] as type hint, # next line will have no warnings. # But if where will be List[A] then warning arrives: # Expected type 'List[A1]', got 'List[A]' instead. # It's Pycharm IDE. process_a1_list(s) def process_a1_list(lst: List[A1]): pass def another_test(): # This is just to see, whether IDE will show warnings or not: # No warning should be on this block good_list: List[A1] = [] help_here(good_list) # On both next calls IDE should warn bad_list_1: List[A] = [] help_here(bad_list_1) bad_list_2: List[int] = [] help_here(bad_list_2)

La única idea que tengo es: Union[List[A1], List[A2]] .

Pero, ¿y si no conozco todas las subclases de A ?

¿Hay alguna manera hermosa de insinuarlo?

Actualización: no es hermoso, pero deja que IDE (Pycharm) verifique los tipos como yo quiero.

Actualización 2: necesito una solución de sugerencias de tipo IDE, no una verificación de tiempo de ejecución. No estoy seguro de que exista la solución que quiero.

Pitón 3.8

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!