question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__array_namespace__ type Hint

See original GitHub issue

I 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:open
  • Created 2 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
rgommerscommented, Dec 2, 2021

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 new getattr behavior will materialize. So I’d say let’s use ModuleType 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.

1reaction
BvB93commented, Dec 15, 2021

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 over object 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found