4

Suppose there's a set of class Map1,Map2,Map3,... all extended from BaseMap, and I use some reflection mechanism to get the child Map's instance. I want to dynamically get an instance of one of these classes and store it in a variable m, and have pydev recognize the type as BaseMap so I can use word completion on it.

I found one solution is to add the code

if False:
    m = BaseMap(0,0,0)

after assigning m and before using it. The line inside the if condition would never be executed, but it declares m is a BaseMap type object.

This may look silly, but it did work. Is there any other way to do it?

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
Max
  • 7,957
  • 10
  • 33
  • 39
  • Related: [Autocompletion in dynamic language IDEs, specifically Python in PyDev](http://stackoverflow.com/q/3482622/95735) – Piotr Dobrogost Sep 12 '12 at 22:11
  • possible duplicate of [Pydev Code Completion for everything](http://stackoverflow.com/questions/6218778/pydev-code-completion-for-everything) The answers on that question are more complete and up-to-date, include the sphinx `:type m: BaseMap` notation that doesn't require assertions. – Joshua Taylor Dec 04 '14 at 22:05

2 Answers2

6

You can use assert isinstance(...) to get autocompletion in pydev on variables where otherwise pydev would not be able to guess the correct type.

Say your code is:

m = getAttr(someThing, 'someAttr')
m.*no autocompletion*

pydev would not be able to know the type of m and therefore won't show the autocompletion.

Try:

m = getAttr(someThing, 'someAttr')
assert isinstance(m, BaseMap) # or whatever class it is
m.*pydev shows autocompletion*

It's somewhat hacky, but it will work (and also does not hurt).

sloth
  • 99,095
  • 21
  • 171
  • 219
0

This question is similar to this post: Eclipse pydev auto-suggestions don't work in some cases

One good answer is already suggested (using asserts). Another solution is to use a constructors as described in this link.

Community
  • 1
  • 1
stacksia
  • 631
  • 6
  • 10