Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add NewType#226

Merged
gvanrossum merged 5 commits into
python:masterpython/typing:masterfrom
ilevkivskyi:masterilevkivskyi/typehinting:masterCopy head branch name to clipboard
Jun 2, 2016
Merged

Add NewType#226
gvanrossum merged 5 commits into
python:masterpython/typing:masterfrom
ilevkivskyi:masterilevkivskyi/typehinting:masterCopy head branch name to clipboard

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented May 27, 2016

Copy link
Copy Markdown
Member

Simple minded implementation of NewType (see #189 and python/mypy#1284 (comment)) plus some tests and a short discussion in the PEP.
Also I added Type and ContextManager to the list of types provided by typing.py.

@bintoro

bintoro commented May 27, 2016

Copy link
Copy Markdown
Contributor

@ilevkivskyi I know you're intent on maintaining the distinction between types and classes, so I think here "class" is the correct choice, no? EDIT: Actually, never mind. I was too focused on how they simulate instantiation. I think there's just something about the name NewType that's bugging me.

From an introspection POV, an instance of a class with def __call__(self, x): return x would be easier to detect than a plain function while still avoiding the instantiation overhead.

@gvanrossum

Copy link
Copy Markdown
Member

@JukkaL, what do you think of this design?

@ilevkivskyi

Copy link
Copy Markdown
Member Author

@bintoro

I know you're intent on maintaining the distinction between types and classes, so I think here "class" is the correct choice, no?

I am intentionally a bit "general" here, see my comment from the issue:
"I do not discuss whether NewType('EURtoUSD', Callable[[float], float]) etc. are allowed. If necessary, we could clarify this later."

Concerning your two other points, the idea here is to be really minimal, to allow minimum overhead. In particular your idea with many base classes is almost equivalent to Intersection, that might be implemented later (there is an issue on this).

@gvanrossum

gvanrossum commented May 27, 2016 via email

Copy link
Copy Markdown
Member

Comment thread pep-0484.txt Outdated
NewType helper function
-----------------------

There are also situations, where a programmer might want to avoid logical

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No comma.

Comment thread pep-0484.txt

* Generic, used to create user-defined generic classes

* Type, used to annotate class objects

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This and the next one I'll apply right now. They're unrelated to this issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, sure, I just posted a link to this PR to python-dev asking for comments.

@JukkaL

JukkaL commented May 27, 2016

Copy link
Copy Markdown
Contributor

A few things:

  • I'd add explicit language about isinstance not being supported for these types (and why not).
  • What should we call types defined using NewType? Are they "NewType types"?

@ilevkivskyi

ilevkivskyi commented May 27, 2016

Copy link
Copy Markdown
Member Author

@JukkaL

I'd add explicit language about isinstance not being supported for these types (and why not).

Good point. I added this in new commit

What should we call types defined using NewType? Are they "NewType types"?

Here I am not sure. Maybe ad-hoc types?

EDIT: replaced by the correct quote

@gvanrossum

gvanrossum commented May 27, 2016 via email

Copy link
Copy Markdown
Member

@bintoro

bintoro commented May 28, 2016

Copy link
Copy Markdown
Contributor

Appointed types — because they are only nominally distinct.

@lgautier

Copy link
Copy Markdown
  • AdhocType- because (presumably) just created for one situation
  • NamedType - to play on a naming similarity with namedtuple

@yedpodtrzitko

Copy link
Copy Markdown

LambdaType

@SpotlightKid

Copy link
Copy Markdown

TypeDef

@sietse

sietse commented Jun 1, 2016

Copy link
Copy Markdown

I like @lgautier's NamedType a lot: to me, it expresses "this is otherwise the same type, but it has a different name (and therefore its own meaning)". Also, like he says, it meshes nicely with namedtuple (and typing.NamedTuple).

How I would read it:

UserID = NamedType('UserID', int)
This is a type of int named UserID


If NamedType does not make it, what about ValueType?

  • ValueType: Scala has the same concept, and calls them Value Classes. It expresses nicely that this type does not say something about the data structure, but about the value it contains.

How I would read it:

UserID = ValueType('UserID', int)
This is an int with a value that is a user ID.

NamedType works better, though.

@gvanrossum

Copy link
Copy Markdown
Member

I could not follow anything from the Scala ValueType description (no news
there :-).

The explanation "it is the same type with a different name" feels wrong --
if it really was the same name it would be a type alias. In fact it is not
the same type, it has a specific relationship to the original type, which
is exactly that it is a subtype (but not every subtype is a NewType).

I will check with Jukka but unless he has strong objections I am going for
Distinct Type (two words) when we're discussing it in English text, but I
will keep NewType (one function) in code.

On Wed, Jun 1, 2016 at 1:42 AM, Sietse Brouwer notifications@github.com
wrote:

I like @lgautier https://github.com/lgautier's NamedType a lot: to me,
it expresses "this is otherwise the same type, but it has a different name
(and therefore its own meaning)". Also, like he says, it meshes nicely with
namedtuple (and typing.NamedTuple).

How I would read it:

UserID = NamedType('UserID', int) -> This is a type of int named UserID


If NamedType does not make it, what about ValueType?

How I would read it:

UserID = ValueType('UserID', int) -> This is an int with a user ID value
type.

NamedType works better, though.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#226 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ACwrMiamuJVBxyBPxt4bA0yULC0M3MzVks5qHUXcgaJpZM4IoPSY
.

--Guido van Rossum (python.org/~guido)

@gvanrossum

Copy link
Copy Markdown
Member

I'm going to abandon the "English description" discussion, and merge this as-is. The new function will be called NewType() and in the discussion we'll use "unique type" (like in this PR).

@gvanrossum

Copy link
Copy Markdown
Member

Mypy issue is here: python/mypy#1284

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.

8 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.