-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EmailMessage missing from email.parser #2417
Comments
We have basically three options here:
Personally, I prefer the second option. |
What about overloads? I think this handles the more common cases:
|
FWIW, I did go with your second option in my most recent project (which forked email for our own reasons). Our
|
Overloads work fine for when a |
The overloads proposed don't actually capture the fact that policies themselves can have arbitrary factories which would require literal types. But we are choosing between imperfect options here. Just returning I took a quick pass at seeing if the override approach was feasible at all with a semi-contrived simplification. My intuition is that, in practice, users of these APIs will likely specify the
These results seem sane to me, though I admit I don't understand what the |
The * suffix in mypy means it's an inferred type, which doesn't really matter unless you're debugging mypy internals. |
This problem could be solved by making MessageT = TypeVar("MessageT")
class Policy(Generic[MessageT]): ...
class Message: ...
class Compat32(Policy[Message]): ...
compat32 = Compat32()
class EmailMessage: ...
class Default(Policy[EmailMessage]): ...
default = Default()
def message_from_string(
string: str,
factory: Optional[Callable[[], MessageT]] = None,
*,
policy: Policy[MessageT] = compat32,
) -> MessageT:
...
... Unfortunately, this is another case where mypy’s insufficient support for generic defaults (python/mypy#3737) gets in the way. But we can work around that bug using overloads. @overload
def message_from_string(
string: str,
factory: Optional[Callable[[], Message]] = None,
) -> Message:
...
@overload
def message_from_string(
string: str,
factory: Optional[Callable[[], MessageT]] = None,
*,
policy: Policy[MessageT],
) -> MessageT:
...
def message_from_string(
string: str,
factory: Optional[Callable[[], Any]] = None,
*,
policy: Any = compat32,
) -> Any:
... |
Is there anything blocking this issue? Could I try to fix this? If yes, what approach should I take? Is making an overload that returns |
If
policy.default
is set, then methods likeemail.message_from_file
will returnEmailMessage
rather than justMessage
The text was updated successfully, but these errors were encountered: