microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.71k stars 765 forks source link

"Code is unreachable Pylance" when it isn't #2981

Closed codeIs4Me closed 2 years ago

codeIs4Me commented 2 years ago

Environment data

Pylance v2022.6.30

Code Snippet

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait

opts = webdriver.ChromeOptions() opts.add_experimental_option("detach", True) driver_location = "./chromedriver" driver = webdriver.Chrome(executable_path=driver_location,options=opts) wait = WebDriverWait(driver, 20) driver.get("https://github.com/")

driver.quit()

XXX

Repro Steps

If you take out the line "driver = webdriver.Chrome(executable_path=driver_location,options=opts)", then the "wait = WebDriverWait(driver, 20)" will be highlighted correctly and obviously the line after that say issue with driver not being defined.

Expected behavior

Should not grayed out my code thinking it is unreachable.

Actual behavior

My code runs and is fine. But VSCode is wrong in saying that all lines after the "driver = webdriver.Chrome(executable_path=driver_location,options=opts)" are unreachable.

Logs

XXX

Screenshot 2022-06-29Pylance

erictraut commented 2 years ago

This is a duplicate of https://github.com/microsoft/pylance-release/issues/2869. It's a bug in older versions of the selenium library. If you upgrade to a recent version (4.2.0), the problem should go away.

bschnurr commented 2 years ago

yes. couldn't repro on newer version

codeIs4Me commented 2 years ago

Had to type this for it to work: pip install selenium==4.2.0 --ignore-installed

erictraut commented 2 years ago

Or you could do pip install selenium --upgrade.

codeIs4Me commented 2 years ago

Thank you for your help.

junyg418 commented 1 year ago

my selenium version is 4.7.2 but this problom happen image image i think this is same stuation...

but The situation was solved because it was updated to 4.8.0 agian update selenium image

Nandinimishra17 commented 7 months ago

I updated selenium in vs code, still its showing the same problem. Help

rchiodo commented 7 months ago

Generally this problem happens when the type information for one of the methods you're calling indicates it will return Never. The intent here is to catch an exception statically. Meaning if you call that code with the arguments provided, it will throw an exception.

This particular issue was with the type annotations for selenium. The older version indicated a return of Never incorrectly, but the newer version fixed that problem.

@Nandinimishra17, it would be better if you opened a new issue. We'd need to have a self contained repro and a list of the packages you have installed.

This works for me with selenium version 4.19.0

image

Nandinimishra17 commented 7 months ago

omg great it worked! i added a new file w the same code in project folder and it worked.Can you explain the logic behind this?

Generally this problem happens when the type information for one of the methods you're calling indicates it will return Never. The intent here is to catch an exception statically. Meaning if you call that code with the arguments provided, it will throw an exception.

This particular issue was with the type annotations for selenium. The older version indicated a return of Never incorrectly, but the newer version fixed that problem.

@Nandinimishra17, it would be better if you opened a new issue. We'd need to have a self contained repro and a list of the packages you have installed.

This works for me with selenium version 4.19.0

image

rchiodo commented 7 months ago

Can you explain the logic behind this?

If you're calling a function that we know is going to never return, it means the code after that can't be analyzed.

Suppose there was a function like so:


def foo():
    raise Exception("Foo crashes")

And then you called foo in your code.


x = foo()

print(x) # This code is marked as unreachable.

In selenium's case, the function definition for webdriver.Chrome was marked as throwing an exception.