Skip to content

Conversation

@uckelman
Copy link

@uckelman uckelman commented Jul 17, 2017

This change fixes Issue #417, by making parse() use duck typing rather than explicitly checking whether it's been given a bytes. Now any object which has a nullary decode() method can be passed to parse().

just a bytes. (In particular, this lets parse() accept a bytearray.)
Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that in Python 2, unicode has a decode method for whatever reason, so I'm not actually sure that this duck typing approach will work.

if getattr(instream, 'read', None) is None:
raise TypeError('Parser must be a string or character stream, not '
'{itype}'.format(itype=instream.__class__.__name__))
if not hasattr(instream, 'read'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do prefer the getattr(instream, 'read', None) is None idiom over hasattr, at least until Python 2.7 support is dropped, based on this article.

Also, why is this being checked at all? Just to avoid streams that have a .decode() method?

@pganssle
Copy link
Member

I'm not really sure how to properly ducktype bytes-like objects in a Python 2.7/3.3-compatible way. I can't actually even find an obvious way to duck-type bytes-like objects in Python 2.7, since both str and unicode have encode() and decode() methods.

I suppose it's not the worst thing in the world to just explicitly add support for bytearray.

jbrockmendel added a commit to jbrockmendel/dateutil that referenced this pull request Jul 22, 2017
Deprecate some unnecessary methods in _timelex
@pganssle pganssle self-assigned this Nov 7, 2017
@pganssle pganssle added this to the 2.7.0 milestone Nov 7, 2017
@pganssle
Copy link
Member

pganssle commented Nov 7, 2017

I'll pull this and try to rebase it against master. I think maybe the right approach here is to use proper duck-typing in Python 3, and use isinstance in Python 2.

@pganssle pganssle mentioned this pull request Nov 12, 2017
@pganssle
Copy link
Member

Fixed in #514. Thank you for your contribution.

@pganssle pganssle closed this Nov 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants