Skip to content

Conversation

@Michael0x2a
Copy link
Collaborator

@Michael0x2a Michael0x2a commented Jul 26, 2016

This pull request implements NewType as described in PEP 484 and in issue #1284. It also adds a variety of test cases to verify NewType works correctly when used and misused and adds information about using NewType to the mypy docs.

This pull request resolves #1284.

@Michael0x2a Michael0x2a changed the title [WIP] Add NewType Add NewType Jul 27, 2016
@Michael0x2a Michael0x2a reopened this Jul 27, 2016
...
However, at runtime, ``NewType('Derived', Base)`` will return a dummy function that
simply returns its argument.
Copy link
Member

Choose a reason for hiding this comment

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

Might want to show this by example, e.g.

def Derived(_x):
    return _x

mypy/semanal.py Outdated

def get_newtype_declaration(self, s: AssignmentStmt) -> Optional[CallExpr]:
"""Returns the Newtype() call statement if `s` is a newtype declaration
or None otherwise."""
Copy link
Member

Choose a reason for hiding this comment

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

Can you make this docstring follow the PEP 257 recommendation?

@gvanrossum
Copy link
Member

Done with the first round of code review. Hopefully it's as thorough as you requested! :-)


.. code-block:: python
def Derived(_x: Base) -> Base:
Copy link
Member

Choose a reason for hiding this comment

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

Since this is runtime, IMO it's better not to annotate it at all: just def Derived(_x): return _x.

@gvanrossum gvanrossum merged commit 2d3dc1e into python:master Jul 28, 2016
@ddfisher
Copy link
Collaborator

ddfisher commented Aug 2, 2016

Thanks for writing such great documentation!

@Michael0x2a Michael0x2a deleted the add-newtype branch August 15, 2016 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type aliases without implicit conversion ("NewType")

4 participants