Skip to content

feat(mypy): infer Exception error type for unit methods on *E aliases (#407) - #2567

Closed
hamid-digitalmarketer wants to merge 3 commits into
dry-python:masterfrom
hamid-digitalmarketer:fix/issue-407-e-alias-unit-methods
Closed

hamid-digitalmarketer wants to merge 3 commits into
dry-python:masterfrom
hamid-digitalmarketer:fix/issue-407-e-alias-unit-methods

Conversation

@hamid-digitalmarketer

Copy link
Copy Markdown

Summary

Fixes #407

Problem

When calling unit methods on container aliases that fix their error type to Exception (e.g. IOResultE.from_value(1), ResultE.from_value(1), FutureResultE.from_value(1)), mypy previously typed the return value as Container[ValueType, Any] rather than Container[ValueType, Exception], because the generic unit constructor cannot infer the unprovided error type parameter and defaults to Any.

Solution

  • Introduced returns.contrib.mypy._features.alias_e.analyze method hook for unit constructor methods across all *E aliases (ResultE, IOResultE, FutureResultE, RequiresContextResultE, RequiresContextIOResultE, RequiresContextFutureResultE).
  • When the constructor is invoked via an *E alias whose bound type fixes the error type to builtins.Exception, any AnyType in that position is rewritten to builtins.Exception.
  • Direct constructor calls on the un-aliased base containers (e.g. IOResult.from_value(1)) are unaffected and retain their default Any error type.

Verification

  • Added test cases in typesafety/test_io/test_ioresult_container/test_ioresult_typecast.yml.
  • Verified type resolution across all *E containers:
    reveal_type(IOResultE.from_value(1))  # => returns.io.IOResult[int, Exception]
    reveal_type(ResultE.from_value(1))    # => returns.result.Result[int, Exception]
    reveal_type(FutureResultE.from_value(1)) # => returns.future.FutureResult[int, Exception]
    reveal_type(IOResult.from_value(1))   # => returns.io.IOResult[int, Any] (preserved)

Contributor Direct Contact

For direct communication regarding this contribution, technical questions, collaboration, or follow-up:
Direct Contact / WhatsApp: +923167417135
Please feel free to contact me directly to discuss the implementation or coordinate further improvements.

@sobolevn sobolevn closed this Oct 29, 2026
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 note

ℹ️ 2 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

✅ 22 untouched benchmarks


Comparing hamid-digitalmarketer:fix/issue-407-e-alias-unit-methods (5cc6eb9) with master (ea76fb5)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Unit method like IOResultE.from_value must return IOResult[_V, Exception] type

3 participants