__array_namespace__ type Hint
See original GitHub issueI hope this falls under the scope of this repo -
Is there any a way type-hint __array_namespace__
? Is it even possible to do so, and it needs to be standardized, or maybe it’s not possible with current python type hinting system?
Motivation:
Suppose I want to write a function that will work in multiple conforming libraries, it will probably start with:
xp = x.__array_namespace__()
or, like in NEP-47:
def foo(x,y):
xp = get_namespace(x,y)
In both cases, I want to be able to type check xp
, and have auto-complete on xp
in the various IDEs. (At least some auto-complete engines relay on static typing e.g https://github.com/microsoft/pyright)
Is this possible?
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
PHP: Type hinting in namespaces not working? - Stack Overflow
PHP: Type hinting in namespaces not working? ... The code basically looks like this: <?php namespace NS; class MySubClass extends MyClass { public ......
Read more >typing — Support for type hints — Python 3.11.1 documentation
PEP 585: Type Hinting Generics In Standard Collections ... x: tuple[*Ts, *Ts] # Not valid class Array(Generic[*Shape, *Shape]): # Not valid pass.
Read more >into-array should not need type hints - Clojure Q&A
Currently into-array's return value needs type hinting in addition to having to specify the target class in the first place.
Read more >Type Hinting in JavaScript - StrongLoop
There's no way to know whether animals is an array, a string, a function, or something else. So how do you make sure...
Read more >Introduction to PHP type-hints for Drupal 8 development
When you are calling a function or method that has an argument(s), you don't always know what type (array, object, string etc) of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah okay, got it now -
types.ModuleType
is already in the stdlib for Python 3.8, so it’s fine for us to use it now. And Mypy et al. supporting the newgetattr
behavior will materialize. So I’d say let’s useModuleType
as the annotation for the return type in implementations where the return type is actually a module.Note that in the standard, the docs now say
**out**: _<object>_
and I’m not sure we can do better there (aside from a recommendation), because the returned object doesn’t need to be a module, it could be a class instance for example.I feel that
Any
would be a small improvement overobject
here; it is designed as the ultimate placeholder, used for situations situation wherein some specific(-ish) type is involved, but you’re not sure how to express it. In contrast,object
is generally reserved for situation wherein truly any arbitrary object is considered valid.