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
{{ message }}
Email example from python docs doesn't validate: Address as EmailMessage header #2863
I don't really like using Any here, it feels like it's defeating the purpose of typing. I prefer the big union _HeaderType as a fix to this problem.
I think BaseHeader (and all the other Header types mentionned above) should be part of the union for _HeaderType, especially since the documentation says that __getitem__ returns a BaseHeader instance more often than not.
I don't know how to make overloads so that if email.headerregistry.HeaderRegistry.use_default_map is True we can type the default mappings correctly.
Also, it would be nice if we were able to deduce the policy from a message, in a way that, for example, message['date'] would be inferred to be an Optional[UniqueDateHeader] for most default policies. But it looks like there's a lot of typing involved for this.
This seems to work because the getitem and setitem have different behaviours : while the getitem mostly returns BaseHeader, the setitem seems to dynamically build a class with a parser which takes any object to build a header from. So I think aliasing _HeaderType to object is not great either
Add BaseHeader to the union of _HeaderType so email.message.EmailMessage__getitem__ can return a BaseHeader without difficulties.
Specify that all specific headers in email.headerregistry are derived from BaseHeader so they can also work there (BaseHeader kind of specifies that the specialized Header have the same interface). This will keep the changes in _HeaderType minimal, and allow all kinds of (Date|Address|...)Header to be used as _HeaderType if need be.
Let email.message.EmailMessage.__setitem__ accept anything
Activity
srittau commentedon Mar 13, 2019
True. Patches welcome!
arjennienhuis commentedon Mar 13, 2019
I'm not sure how. Maybe:
_HeaderType = Union[str, Header, ContentTransferEncodingHeader, ContentTypeHeader, UniqueDateHeader, SingleAddressHeader, AddressHeader, UniqueAddressHeader, UniqueSingleAddressHeader, MIMEVersionHeader, UniqueUnstructuredHeader, DateHeader, ContentDispositionHeader]Or just:
_HeaderType = Anyarjennienhuis commentedon Mar 13, 2019
msg['From'] = 2actually runs, so maybe:_HeaderType = objectmessage.pyi: Now takes Address as EmailMessage header.
gagbo commentedon Apr 16, 2019
I don't really like using
Anyhere, it feels like it's defeating the purpose of typing. I prefer the big union_HeaderTypeas a fix to this problem.I think
BaseHeader(and all the other Header types mentionned above) should be part of the union for_HeaderType, especially since the documentation says that__getitem__returns aBaseHeaderinstance more often than not.I don't know how to make overloads so that if
email.headerregistry.HeaderRegistry.use_default_map is Truewe can type the default mappings correctly.Also, it would be nice if we were able to deduce the policy from a message, in a way that, for example,
message['date']would be inferred to be anOptional[UniqueDateHeader]for most default policies. But it looks like there's a lot of typing involved for this.gagbo commentedon Apr 16, 2019
This seems to work because the getitem and setitem have different behaviours : while the getitem mostly returns BaseHeader, the setitem seems to dynamically build a class with a parser which takes any object to build a header from. So I think aliasing
_HeaderTypetoobjectis not great eitherarjennienhuis commentedon Apr 16, 2019
But there is this:
msg['Date'] = datetime.datetime(2020, 2, 2)arjennienhuis commentedon Apr 16, 2019
So
__getitem__can return aBaseHeaderbut__setitem__should acceptobjectorAnygagbo commentedon Apr 16, 2019
I'm okay with this too :
BaseHeaderto the union of_HeaderTypesoemail.message.EmailMessage__getitem__can return a BaseHeader without difficulties.email.headerregistryare derived fromBaseHeaderso they can also work there (BaseHeaderkind of specifies that the specialized Header have the same interface). This will keep the changes in_HeaderTypeminimal, and allow all kinds of(Date|Address|...)Headerto be used as_HeaderTypeif need be.email.message.EmailMessage.__setitem__accept anythingutkarsh2102 commentedon Apr 17, 2019
This workaround makes more sense to me, instead of the above one, no?
At the moment, #2924 implements
Any, let me know how should I edit the same.arjennienhuis commentedon Apr 18, 2019
Returning a Union is not ok. See python/mypy#1693
message.pyi: Now takes Address as EmailMessage header.
message.pyi: Now takes Address as EmailMessage header.
message.pyi: Change HeaderType to Any (#2924)