Python VS code detect incorrect argument name - python

How can I make VS code detect incorrect argument names?
def test_function(name1, name2, name3):
print(name1)
print(name3)
print(name2)
test_function(name1=1, name2=2, name4=4)
Currently, VS code doesn't flag this as an error. Detecting this would be very useful since argument names change. Otherwise you get an error at run time which is far too late.

VS code is a text editor that one can use to write code in multiple languages but is not a Python interpreter. What you can do is install an extension to do that. For example: Python from Microsoft, has a linter that shows you what problems your code has, including the kind of problem you're describing. Make sure you read the docs to configure it correctly.

Related

How to change language syntax highlighting without affecting run option?

I use the .pyde filetype to make processing.py code, it's jpython so basically it's typed the same as Python however needs a different command to run than python. However every time I set the language mode to Python it changes the command to run, any ideas?
Alright so apparently there's, like, defaults that it adds or something, so even commenting out python in the executor map [which has higher priority than extension map] doesn't help, you literally need to have "python": null in there example

PyScripter Python 2.7 - summarize lines of code into one line (visually)

I was previously working on C++ on VisualStudio and when I was creating a Class, a Function or some lines of code, I remember I could "summarize" all the lines relative to one function for example into only one line that had the name of the function and a little "+" on the side and when you clicked on it, it showed you the entire function code associated with. It was really usefull in order to see clearly through a script not by showing the entire (non necessary) code.
I was wondering if it's possible with Python on PyScripter and how to do this? I have a lot of functions or lines that I would like to "hide" and just summarize as one (describing the name of the function, or just a comment) to see more clearly through my code.
Thank you.
This is called code folding. Recent versions of PyScripter provide code folding. Download PyScripter from https://sourceforge.net/projects/pyscripter/files.

How can I check if a string is only letters in python using isalpha?

I am trying to check to see if a user's input only contains letters. Other forums seem like the 'isalpha' function seems to be what I'm looking for, yet when I use it in my code and run it, an error appears saying AttributeError: 'str' object has no attribute 'isalpha'.
This is my code:
name='hello'
if name.isalpha():
print("Your name is valid")
I don't understand where I'm going wrong.
As most people in the comment have mentioned that tool you have online does not work properly and you should stay away from it.
From the website you provided:
This is a simple online Python interpreter, built using the Skulpt
engine (slightly modified by kwalsh).
Put your Python code below (copy-and-paste or just type it in
directly), then click run.
Note: this isn't a complete version of Python. See below.
If you must use an online interpreter, you might try out the engine that is provided by your school (Skulpt).
But I highly recommend you get python installed onto your laptop, you're less likely to run through any errors.

Pycharm's code style inspection: ignore/switch off specific rules

I'm trying to import existing project into PyCharm. I can refactor the code so that PyCharm will be pleased, but we like to have spaces around colons in dictionaries, like this: {"A" : "B"}. We also like aligning assignments:
a = 1
abc = 3
Is there a way to configure PyCharm, so that he'll ignore all errors/warnings related to this specific stuff?
Using PyCharm 5 (community edition), you can do the following: Code –> Inspect Code. Then select the required inspection error, and click on the "Suppress" option or "Ignore errors like this" option on right hand side.
Please look at the screenshot below:
When you choose the "Suppress" option, it adds a comment as shown in the screenshot below:
Suppressing can be done at the statement, or function/method, levels. If trying to suppress an argument to a function, the suppression only works at the function level (meaning it also suppresses other name reference violations that might exist within that function).
You also have the option of switching off "PEP8 coding style violations" altogether (by ticking the box shown below), or explicitly managing "Ignore Errors" manually. Screenshot given below:
In general, you should perhaps question why you are suppressing PEP8 guidelines. However, sometimes it appears necessary, for instance when using the pytest module, it is necessary to shadow variables, etc, which the PEP8 Inspection complains about in. In such cases, this feature in PyCharm is very helpful.
If you're ok to ignore all matching issues, you can just press Alt-Enter (or click on the lightbulb) and choose "Disable Inspection". Saves you going into the settings and trying to figure out the inspection rule that matches.
From http://iambigblind.blogspot.jp/2013/02/configuring-pep8py-support-in-pycharm-27.html
#Krzysztof Stanisławek, function is different as Pycharm follows the PEP8 coding style, so it is recommended that there is no whitespace between the function variables and ":", if you don't want this, you can disable it by
"Settings"-> "Editor"-> "Inspections"->"PEP8 coding style violation"
However, this is not recommended.
I got the same issue, and the neat solution has been pointed by #fantabolous, from configuring PEP8.py support in PyCharm 2.7
Example before
Adding the error code either manually or with "Alt+Enter" on error highlight
The error code can be found here
After the changes
It's great to select precisely some rules instead of disabling all warning from PEP8. Thanks to the previous comments.
to have spaces around colons in dictionaries, configure Settings > Editor > Python Spaces
Other > Before ':'
and
Other > After ':'

Code completion not giving recommendations

Say I'm working with the 'requests' Python library.
req = requests.get("http://google.com")
Now after this, if I type req., I'm supposed to get a list of all methods I can access. But for some reason I don't, even if I manually press Ctrl+Space.
If I try this in iPython, I get autocomplete recommendations. Even if I try it via the built in Python console in PyCharm, I get recommendations.
Why's this happening?
As Python is a dynamically typed language, you need to ensure it can work out what type things are, and inspect on the libraries on your system correctly. Try to make sure it's obvious what type the object is in your code.
One good way as of PyCharm 2.7 (back when versions were numbers) is to enable runtime type detection - PyCharm hooks into your program while it runs (while debugging), and checks the types of variables as they are used.
You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight".
Obviously it is worth noting that this isn't perfect - if you make changes, this won't be updated til the code is executed, and it can only tell you about values it has seen - other code paths you haven't tried could set other types.
You can also 'tell' PyCharm by using Epydoc or Sphinx style docstrings that contain information about parameter and return value types. PyCharm will use these to improve it's inspections.
Python also gained support for function annotations as of Python 3. These can be used for type hints as per PEP 484. See the typing module for more. This is more formal, so it can also be used for tools like mypy which a type checker that can programmatically check these types for consistency, giving Python a TypeScript-style optional static typing.
Python is a dynamically typed language, which means that the "get" function does not declare its return type. When you're entering code in IPython or in the PyCharm console, the code is actually being executed, and it's possible to inspect the object instance in the running interpreter and to get the list of its methods. When you're entering code in PyCharm or in any other Python IDE, it is not executed, and it's only possible to use static analysis to infer the return type of the method. This is not possible in all cases.
PyCharm has no idea what the dict contains if you fill it dynamically. So you have to hint PyCharm about the keys of dict beforehand. Prodict does exactly this to hint PyCharm, so you get code completion.
First, if you want to be able to access the response object, then you have to get a json response and convert it to dict. That's achieved with .json() method of requests like this:
response = requests.get("https://some.restservice.com/user/1").json()
OK, we loaded it to a dict object, now you can access keys with bracket syntax:
print(response['name'])
Since you ask for auto code completion, you certainly need to hint PyCharm about the keys of dict. If you already know the respone schema, you can use Prodict to hint PyCharm:
class Response(Prodict):
name: str
price: float
response_dict = requests.get("https://some.restservice.com/user/1").json()
response = Response.from_dict(response_dict)
print(response.name)
print(response.price)
In the above code, both name and price attributes are auto-complated.
If you don't know the schema of the response, then you can still use dot-notation to access dict attributes like this:
response_dict = requests.get("https://some.restservice.com/user/1").json()
response = Prodict.from_dict(response_dict)
print(response.name)
But code-completion will not be available since PyCharm can't know what the schema is.
What's more is, Prodict class is derived directly from dict, so you can use it as dict too.
This is the screenshot from Prodict repo that illustrates code completion:
Disclaimer: I am the author of Prodict.
if will just detect methods or variables and... with write some part of it:
File->Setting -> Editor -> General -> Code Completion
in top of opened window , unCheck [ Mach Case ]
It's an old question but probably all the provided answers missed the mark by a margin as wide as Sun's distance to Betelgeuse (none of the answers is accepted and #user1265125 is an active guy with 8 yrs here and more cred than me).
As it happens, I've just had exactly the same problem as OP and the solution was:
A NON-ASCII CHAR SOMEWHERE IN THE PROJECT'S FOLDER PATH
Seriously, PyCharm devs...[doubleFacepalm]
In my case the solution is to reset the settings, everething else wasn`t working for me.
"From the main menu, select File | Manage IDE Settings | Restore Default Settings.Alternatively, press Shift twice and type Restore default settings."
I had a similar problem. Only functions I had already used were suggested and only as plain text and not recognised as methods.
What fixed that for me was deleting the /.idea folder in the project directory. (Afterwards you will have to set your run configurations again)
With the latest version update to 2022.2, even auto-complete stopped working for me. After quite a bit of reading articles, I just found the https://youtrack.jetbrains.com/issue/PY-50489 issue which was the root problem. The old plugins were pending update, after that, the code completion issue was fixed.
So, try and check if you are facing the same problem, if the plugins are up to date in Settings —> Plugins.

Categories

Resources