You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That just might work. There are still some details that are non-obvious -- for example, what happens if the type of an attribute or a method argument is different is m1.C and m2.C. Also, generating useful error messages when accessing an attribute defined in m1.C but not m2.CC, for example, would need some extra logic.
Implementation-wise, we could internally create a new class that is a subclass of only the common base classes of m1.C and m2.CC, and only includes definitions that are implemented by both of the classes. The class would not be a subclass of either m1.C or m2.CC, which might be a problem.
Wouldn't it effectively be treated as an Union[C, CC] for the code that performs this conditional import?
No, that fails to ensure that every instance is of the same type (thus causing problems for binary operator overrides, among other things). However, I would think that TypeVar('C', m1.C, m2.CC) would be entirely suitable for this use case.
Activity
refi64 commentedon Jan 8, 2016
Maybe Mypy could set
C's type to the intersection (all the common attributes/methods) ofm1.Candm2.CC.JukkaL commentedon Jan 9, 2016
That just might work. There are still some details that are non-obvious -- for example, what happens if the type of an attribute or a method argument is different is
m1.Candm2.C. Also, generating useful error messages when accessing an attribute defined inm1.Cbut notm2.CC, for example, would need some extra logic.Implementation-wise, we could internally create a new class that is a subclass of only the common base classes of
m1.Candm2.CC, and only includes definitions that are implemented by both of the classes. The class would not be a subclass of eitherm1.Corm2.CC, which might be a problem.lkraider commentedon Mar 2, 2017
Wouldn't it effectively be treated as an
Union[C, CC]for the code that performs this conditional import?NYKevin commentedon Nov 25, 2017
No, that fails to ensure that every instance is of the same type (thus causing problems for binary operator overrides, among other things). However, I would think that
TypeVar('C', m1.C, m2.CC)would be entirely suitable for this use case.JukkaL commentedon Jan 28, 2020
This seem too difficult to fix relative to the benefit. Generally using
# type: ignoreis an acceptable workaround.