Replies: 2 comments 14 replies
-
Can you describe the problem you're seeing in more detail? You mentioned in the other thread that the ellipses make mypy unhappy. When I paste your code into a new document, add the missing import statements, and run mypy on it, I don't see any errors. Normally, you'd need to supply real default argument values rather than using an ellipsis, but type checkers generally allow you to use an ellipsis as a stand-in for the actual value when you're writing overloads or stubs. It's still a good idea to provide the actual default argument values because it allows language servers to show users these values when they call the function. For example, here's what I see when I start typing a call to It would be better if you supplied the actual default values. I presume they are zero by default? If so, replace the |
Beta Was this translation helpful? Give feedback.
-
Ambiguity in Method Resolution: Problem: When methods are overloaded with flexible types (e.g., #seq<_> or #exn in F#, or generic types in Python), the compiler or runtime may struggle to resolve which method to call due to overlapping type hierarchies or ambiguous signatures. For example, in F# (), overloading methods with flexible types like #Uri and #Stream can trigger errors like FS0438: Duplicate method. Language-Specific Overloading Limitations: Python: Python does not support traditional method overloading because it treats methods as attributes, and the last defined method with a given name overwrites previous ones (). This makes it difficult to implement overloads for flexible types without workarounds. Flexible Types and Type Inference Issues: Problem: Flexible types (e.g., #T in F# or generics in Python with @overload) complicate type inference. For instance, in Python’s mypy with @AbstractMethod and @overload, requiring an implementation for abstract methods can clutter the code (). Code Readability and Maintenance: Problem: Overloading methods, especially with flexible types, can make code harder to read and maintain. As noted in, overloading can introduce concealed semantics, leading to bugs or misunderstandings in method behavior. Incompatibility with Abstract or Generic Classes: Problem: In abstract classes or interfaces, overloading methods with flexible types (e.g., Python’s @AbstractMethod with @overload) may require unnecessary implementations or cause type checker errors (). Professional Solutions and Best Practices Resolve Ambiguity with Explicit Signatures: Solution: Ensure method signatures are distinct in terms of parameter types, number, or order. In Java or C#, use specific types rather than overly flexible ones (e.g., List instead of List<?>) to avoid ambiguity (,). Workarounds for Python’s Overloading Limitation: Solution: Since Python doesn’t support traditional overloading, use a single method with optional parameters, default values, or type checking (). Alternatively, use the @overload decorator with typing for static type checkers like mypy. class Calculator: Handle Flexible Types in Abstract Classes: Solution: For abstract classes with flexible types (e.g., Python’s @AbstractMethod with @overload), define a generic base implementation to satisfy the type checker, as suggested in. Alternatively, use generic types to parameterize the class. T = TypeVar('T') class Base(ABC, Generic[T, U]): class Concrete(Base[int, str]): Improve Readability and Avoid Overloading Pitfalls: Solution: Limit overloading to cases where it improves usability without introducing complexity. As suggested in, consider using distinct method names if overloads risk confusion or bugs. F# Workaround for Flexible Types: Solution: For F#’s flexible type issues (), either enumerate specific implementations for each type or use different method names. If overloads are necessary, constrain flexible types explicitly. Leverage Type Systems and Static Analysis: Solution: Use static type checkers (e.g., mypy for Python, or IDE tools for Java/C#) to catch overloading issues early. In Python, ensure @overload definitions are consistent with the implementation (). website: https://thedifferentlanguages.org/ Refactor to Reduce Overloading: Solution: If overloading becomes excessive, refactor to use polymorphism, interfaces, or design patterns like Strategy or Factory to handle flexible behavior (). class AddOperation : IOperation { class Calculator { Recommendations Choose the Right Tool for the Job: If overloading is problematic in your language (e.g., Python), consider alternative approaches like default parameters or generics. In F#, avoid flexible types in overloads unless absolutely necessary. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
I eventually managed to release a
py.typed
version of testfixtures, but to do so I had to give up on a bunch of overloads which I thought helped document intended methods of use:simplistix/testfixtures@70283c2
I've tried to get my head around this a few times over the last few months:
I even ended up logging a
mypy
bug:...and as well as several attempts myself, LLMs have also been struggling, even once the underlying typing was sorted out:
At a high level, I'm essentially looking to revert simplistix/testfixtures@70283c2 but with functioning type annotations, essentially providing these three calling patterns for the factory:
...and then these overload patterns for the methods for manipulating the queue of upcoming mock datetimes:
...and similar across
mock_datetime
,mock_time
andmock_date
and the helper types they construct.Is Python typing just "not there yet" or am I missing something? Happy to be shown the way, ecstatic if someone feels inspired to throw up a PR 😅
Beta Was this translation helpful? Give feedback.
All reactions