antonagestam / phantom-types

Phantom types for Python.
https://pypi.org/project/phantom-types/
BSD 3-Clause "New" or "Revised" License
204 stars 9 forks source link

Incompatibility with `NewType` #296

Closed akefirad closed 6 months ago

akefirad commented 6 months ago

Is this a bug or feature:

MoneyAmount = NewType("MoneyAmount", Natural)

Calling the parse method of MoneyAmount fails with

 AttributeError: 'NewType' object has no attribute 'parse'

Any way to make phantom types compatible with NewType? Thanks.

antonagestam commented 6 months ago

Is this a bug or feature?

Neither, NewType is a completely distinct feature from this library. You could say it's a feature of NewTypes that they work this way though.

A better way to look at it is that phantom types are a more powerful alternative than new types. I've written this blog post that discusses some related things.

antonagestam commented 6 months ago

If you want the resulting type to have the parse method, what you should do rather than using NewType is likely this:

class MoneyAmount(Natural): ...