Skip to content

Union of a type and the typing module function #88808

Closed

Description

@serhiy-storchaka
BPO 44642
Nosy @gvanrossum, @serhiy-storchaka, @sobolevn, @Fidget-Spinner

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-01-23.07:25:17.626>
created_at = <Date 2021-07-15.07:58:18.901>
labels = ['interpreter-core', '3.10', '3.11']
title = 'Union of a type and the typing module function'
updated_at = <Date 2022-01-23.07:25:17.625>
user = 'https://github.com/serhiy-storchaka'

bugs.python.org fields:

activity = <Date 2022-01-23.07:25:17.625>
actor = 'sobolevn'
assignee = 'none'
closed = True
closed_date = <Date 2022-01-23.07:25:17.626>
closer = 'sobolevn'
components = ['Interpreter Core']
creation = <Date 2021-07-15.07:58:18.901>
creator = 'serhiy.storchaka'
dependencies = []
files = []
hgrepos = []
issue_num = 44642
keywords = []
message_count = 8.0
messages = ['397528', '397558', '397569', '397570', '397632', '411298', '411330', '411351']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'serhiy.storchaka', 'sobolevn', 'kj']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue44642'
versions = ['Python 3.10', 'Python 3.11']

Activity

  1. serhiy-storchaka commented on Jul 15, 2021

    @serhiy-storchaka
    MemberAuthor

    The union type accepts any function from the typing module:

    >>> import typing
    >>> int | typing.cast
    int | typing.cast
    >>> int | typing.get_type_hints
    int | typing.get_type_hints

    It is a consequence of too lenient check for the typing.NewType() result (which does not work at all with PR 9951).

  2. gvanrossum commented on Jul 15, 2021

    @gvanrossum
    Member

    Is it worth being picky about this? The internal data structures are safe. A static checker will complain. What more do you need?

  3. serhiy-storchaka commented on Jul 15, 2021

    @serhiy-storchaka
    MemberAuthor

    It is only reminder that our test for NewType is arbitrary. And it is not compatible with PR 9951. Possible solutions are:

    1. Make NewType a class and add more strict test. Pro -- we can make arbitrary repr, contra -- it has small negative effect on performance. See bpo-34963.

    2. Remove the test for NewType at all and allow or-ing types and arbitrary functions.

    3. Introduce a special attribute to distinguish unionable objects. It will make the code of unionobject.c simpler.

    I lean towards option 1 (not excluding 3). Small performance losses seem inevitable.

  4. Fidget-Spinner commented on Jul 15, 2021

    @Fidget-Spinner
    Member

    @serhiy

    1. Make NewType a class and add more strict test.

    See also: bpo-44353 (https://bugs.python.org/issue44353). We had some discussion there about converting to class but it ended in a deadlock.

  5. gvanrossum commented on Jul 16, 2021

    @gvanrossum
    Member

    I think making NewType into a class isn't a good idea, it would be too slow. But I like bpo-34963 (PR 9951).

    So then let's do some variation on (3) -- unionable things would include all types (of course), typevars, and things that have a special attribute. We could then set that special attribute on the function returned by NewType (like PR 9951 already does for __name__ and __qualname__).

    What to call it? Maybe "__unionable__"?

  6. sobolevn commented on Jan 22, 2022

    @sobolevn
    Member

    It does not happen anymore on main (3.11):

    Python 3.11.0a4+ (heads/main-dirty:ef3ef6fa43, Jan 20 2022, 20:48:25) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import typing
    
    >>> int | typing.cast
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for |: 'type' and 'function'
    
    >>> int | typing.get_type_hints
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for |: 'type' and 'function' 
    
  7. gvanrossum commented on Jan 23, 2022

    @gvanrossum
    Member

    Do you recommend to just close this as "fixed"?

  8. sobolevn commented on Jan 23, 2022

    @sobolevn
    Member

    Looks like it was fixed indeed, NewType is now a class. And I cannot reproduce it even on 3.10:

    Python 3.10.0 (default, Nov  1 2021, 10:24:06) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import typing
    
    >>> int | typing.cast
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for |: 'type' and 'function'
    
    >>> int | typing.get_type_hints
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for |: 'type' and 'function'
    
  9. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions