I received this error in Pycharm Community Edition 5.0.3 today and was wondering whether it was just something I'm doing wrong/not realizing, or if it is a PyCharm lint issue. The code to reproduce the error is
mylist = list()
# fill mylist, or do nothing here, either way the error persists
if mylist:
# if something in the list...
mylist.append(2)
else:
# list is empty, add something
mylist.append(1)
# warning at the loop here
for val in mylist:
print val
Is this because it's thinking that mylist is a union of type ?
Optional[list] is a typing annotation that signals PyCharm found mylist to be either None or a list object.
This appears to be caused by the if mylist: test; I'd say this is PyCharm making an incorrect inference here, since you clearly set mylist to a list instance only one line before. if tests for emptiness, not if the object is None.
This is a bug, filed with the PyCharm project as issue PY-21897, and fixed in PyCharm verson 2017.1.
If you place something like this:
""":rtype: list"""
in the docstring for the function that produces your list, that should make pycharm realize you really are expecting a list.
Related
How to make auto-completion list all (static) fields of a certain class that has appropriate variable-type when ctrl+space at a slot of parameter in a certain function?
Example
I tried to ctrl+space in the below code :-
(Code as text is here.)
Question: How to make it show E_1 E_2 E_3?
I don't mind another plugin if I really need one.
It currently works but only for enum :-
My workaround
In practice, to get smart clue, I have to type more (PrototypeList::) :-
Bounty Reason
Here is the result of the current answer (citizenmatt's):-
It is different, but still not show E_1 E_2 E_3.
Have you tried Smart Completion? This feature will only show completion items that are valid for the current context. I think it works in C++, too.
In fact, ReSharper does help you here. All of E_1, E_2 and E_3 are in the completion list, but not on the top of it - they are assigned lower scores because they need an additional qualifier. That said, looks like there is still an issue with scoring:
E_2 and E_3 are in the list too, but they are not shown alongside E_1. We'll investigate this (RSCPP-19501).
I am using Kivy to build a simple app that would load different images in different tabs of a tabbed panel. The different Panel items should all behave similarly, but with different images, so I created a widget class. I am trying to initialize my app using the kv language like in many examples.
Currently, I am unable to make it work, because I cannot find how to pass the file names in a list from the kv language part to the widget instance. I am able to work with other Properties, but the ListProperty has me stumped.
Here is a snippet from my code:
Builder.load_string("""
<MyMainClass>:
#stuff
TabbedPanelItem:
MyClassLayout:
filenames: ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'pic4.jpg']
#other TabbedPanelItems like the one above,
#with different strings in the list
""")
def MyMainClass(TabbedPanel):
pass
def MyClassLayout(FloatLayout):
filenames = ListProperty([])
#rest of my class
Things I already tried:
Use different parentheses in assigning the list in the kv language part: I tried () and {}, as well as with no parentheses.
Initialize the ListProperty differently: I tried putting some string in it already.
Send different lists: I tried sending numbers instead of strings.
The result is always that the filenames list in my widget is always at the default value. That would be [] in the snippet above, or whatever I set in its declaration in my class.
Would someone please point out what it is I am doing wrong?
Thanks.
I managed to fix this.
The issue was that I was trying to read the lists in the constructor. However, they receive their value from the kv lang part after the widget object has finished its constructor.
As a fix, I call the method that reads the list like so:
Clock.schedule_once(self.late_init, 0.02)
I hope people find this and it helps them.
I am using haystack with elasticsearch. I have build index data using rebuild_index command. But when I tried to search for object, its giving me following error:
"Object could not be found in database for SearchResult ' (pk=u'118')>'."
I have double checked in database, no records were deleted. But I am still getting this error.
Can anyone please help with it?
Thanks.
It looks like you are searching for your pk as a string and not an integer, not sure what your query looks like.
as integer, notice no quotes when defining pk_int:
pk_int = 118
Model.objects.get(pk = pk_int)
if your variable containing the pk is a string already, use int():
pk_string = '118'
Model.objects.get(pk = int(pk_string))
Answering it to mark it resolved.
It was database config issue. I am having different settings file for different environment, and was having mismatch for database config in both files.
For anyone coming from a web search engine, if the accepted answer doesn't work for you, you can also try rebuilding the index:
./manage.py rebuild_index
I had the same error message as the OP, and rebuilding the index solved my issue.
I'm reaching the end of the first page of the Django tutorial. I tried a quick experiment, and since it hasn't worked I'm confused. Following along with the tutorial, I have a variable p:
p = Poll.objects.get(pk=1)
Rather than creating a poll using p.choice_set.create(choice='Not much', votes=0) as the tutorial instructs, I tried:
x = Choice(choice='Not much', votes=0, poll=p)
Having done this I would have thought that p.choice_set.all() would return something more than an empty list. But it does return an empty list.
(However, if I try x.poll then I get <Poll: What's up?> as I would have expected, so the relationship is only working one way it seems.)
I'm sure there's a good reason why this doesn't work, even though it seems like it ought to! (please bear in mind I have no database experience)
Any thoughts welcome
x = Choice(choice='Not much', votes=0, poll=p) creates an instance of a Choice model but it is not yet saved to the database. p.choice_set.all() queries the database for choices which are associated with the given poll. Since x was not saved to the DB it will not be found.
I've been having a very strange problem.
I have a test class that subclasses django.test.TestCase which has about 5 different tests in it.
When I run my full test suite (using nose, and specifying sqlite as backend) there are a series of failures. When I go to debug the tests, running them individually, they pass fine.
In one of my tests I get the count of objects before adding an additional object. ex.
test_count = TestObject.objects.all().count()
# Add an entry to TestObject
self.assertEqual(test_count + 1, TestObject.objects.all().count()) # should pass
This was confusing that it would work fine when run individually but not when run with other tests.
In pdb when I look at the variables, test_count is equal to 1, but TestObjects.objects.all().count() is equal to [] after the first line.
ipdb> test_count
1
ipdb> TestObject.objects.all()
[]
ipdb> TestObject.objects.all().count()
0
This takes place right on the second line after assigning value to test_count but before adding another object to TestObject
When my coworker runs our tests all of his pass fine.
Has anyone experienced things of this nature before? I have tried to change the variable names, I thought maybe there was a conflict somewhere. I am all out of ideas. Thank you for your help.
According to the SO post, you're seeing TestObject.objects.all().count()
return two different values on successive calls.
That is basically impossible unless there's something else getting in there
and doing stuff to your db.
Alternatively, does your subclass of TestCase correctly call
super(MyTestCase, self).setUp() and other superclass methods to get the DB
setup right?
Malcolm
posted from django-users