Skip to content

official api method to check if a Type is parametrized or not #423

Closed

Description

@smarie

Hello there,

I am relying on PEP484 in a declarative parser/converter registry project, where the user can e.g. say "please parse a Dict[str, Tuple[pd.Dataframe, pd.Series]] from this directory".
In several places in the code I end up having to check if a given Type is parametrized or not, and find its 'base' non-parametrized type.

At present there is an unofficial way to do that : check if the cls.__origin__ field exist and is not None. Maybe this should be worth putting in the official API with a couple methods such as is_parametrized, and get_base_type. What do you think ?

Thanks for this great tool

Activity

  1. smarie commented on May 4, 2017

    @smarie
    Author

    Also I forgot to mention that checking if a Type is generic is also tricky. Of course you can use issubclass(cls, Generic) but is does not work with all types : for example issubclass(Any, Generic) raises a TypeError. And it returns true for non-typing classes : issubclass(str, Generic) returns true...
    So here again, a simple is_generic method in the API would help (or, making issubclass work for all types)

  2. markshannon commented on May 4, 2017

    @markshannon
    Member

    issubclass is for testing whether a class is a sub-class of another. If you want to check whether a type is a subtype of another you will need to use a type-checker.

  3. smarie commented on May 4, 2017

    @smarie
    Author

    Thanks @markshannon ; not sure I understand your answer since a couple of sources on the internet seem to say that the class/type difference does not exist anymore. But anyway this is not my point - my point here is that, as users, we should be able to access to higher-level information in a quite stable way (such as knowing if a typing type is generic, knowing its parameters if it is parametrized, etc.). Since I started my development a couple months ago, the internal way to represent types in typing module has changed quite a bit (the most important change being 2589b22, but it is not the only one - for example a parametrized Tuple parameters used to be stored in __tuple_params__, now they are in __args__, etc.).

    And it might change again in the future, for sure - that's reflecting how dynamic the language is 👍 . However the concepts of generic type, parametrized type, and parameters are probably stable now - hence the suggestion to provide a few stable documented methods in the API to access this information.

    Kind regards

  4. JelleZijlstra commented on May 4, 2017

    @JelleZijlstra
    Member

    This looks like a use case for typing_inspect (#377).

  5. ilevkivskyi commented on May 4, 2017

    @ilevkivskyi
    Member

    @smarie
    The typing module still has the provisional status, so that there may be changes in API until Python 3.7 is released next year. The typing_inspect sub-project (see #377 mentioned by @JelleZijlstra ) is designed exactly for your needs. It will be released as a separate package on PyPI within few days. When the API stabilizes, it will be made a part of the typing module.

  6. smarie commented on May 4, 2017

    @smarie
    Author

    This is great, thanks a lot @ilevkivskyi ! I'll have a try when it's ready.

  7. ilevkivskyi commented on May 9, 2017

    @ilevkivskyi
    Member

    The typing_inspect is now published as a separate package:
    https://pypi.python.org/pypi/typing-inspect
    https://github.com/ilevkivskyi/typing_inspect

    Please try it and comment on the API. After the API is settled, some introspection functions may be added directly to typing.

  8. smarie commented on Jun 29, 2017

    @smarie
    Author

    Thanks for the proposal @ilevkivskyi ; I'll have more time to have a look in the upcoming weeks and try to provide constructive feedback.

  9. smarie commented on Sep 13, 2017

    @smarie
    Author

    So I finally found some time to try. It works, smarie/python-parsyfiles#4 is now closed on my side.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions