Git Product home page Git Product logo

Comments (5)

ambv avatar ambv commented on May 27, 2024

The core issue here is that we're trying to create an add-on to the language without modifying its syntax. Having X = TypeVar('X') work but not 'Y = TypeVar('X')` is confusing. Seems like we'd really want TypeVar to be a statement in the local scope. We could make it a "statement", e.g.

Var('X')
Var('AnyStr', bytes, str)

However, it would need to inject the name into the local scope which is just as ugly, I guess.

By the way, why does importing type variables not work?

from typing.

gvanrossum avatar gvanrossum commented on May 27, 2024

I propose to use mypy's rules. Every TypeVar() call creates a unique type variable. The static checker enforces that TypeVar() must always be assigned to a local (or global, depending on the current scope) variable whose name is the same as the string passed as argument. The runtime doesn't enforce this, but it still treats each TypeVar() as a unique type. For example:

T = TypeVar('T')
X = TypeVar('T')  # Fails static check, but OK at runtime

Now the following asserts all pass:

assert issubclass(T, T)
assert issubclass(X, X)
assert not issubclass(T, X)
assert not issubclass(X, T)
assert T != X

(Note that the first two simply check that issubclass of a type with itself should always be true.)

from typing.

gvanrossum avatar gvanrossum commented on May 27, 2024

Oh, my comment about import was simply that mypy will (of course) let you define type vars with the same name in multiple modules, and you can import them, but they will still be considered different. E.g. this passes in mypy:

from typing import AnyStr
def make_url(host: AnyStr, path: AnyStr) -> AnyStr:
    if isinstance(host, str):
        return 'https://' + host + path
    if isinstance(host, bytes):
        return b'https://' + host + path
print(make_url('www.python.org', '/dev/peps/'))

I can also systematically substitute typing.AnyStr for AnyStr.

It also works if I replace the import with:

import typing
AnyStr = typing.typevar('AnyStr', values=(str, bytes))

But after that if fails if I replace the return type with typing.AnyStr:

import typing
AnyStr = typing.typevar('AnyStr', values=(str, bytes))
def make_url(host: AnyStr, path: AnyStr) -> typing.AnyStr:
    if isinstance(host, str):
        return 'https://' + host + path
    if isinstance(host, bytes):
        return b'https://' + host + path
print(make_url('www.python.org', '/dev/peps/'))

The errors are:

z1.py: In function "make_url":
z1.py, line 5: Incompatible return value type: expected builtins.bytes*, got builtins.str
z1.py, line 7: Incompatible return value type: expected builtins.str*, got builtins.bytes

from typing.

ambv avatar ambv commented on May 27, 2024

OK, I understand. Will update the PEP accordingly.

from typing.

JukkaL avatar JukkaL commented on May 27, 2024

I updated the PEP at PyCon to describe the new type variable semantics. I think that this can be closed now.

from typing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.