microsoft / pylance-release

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

Can't use module as a type defination? #1956

Closed r-priyam closed 3 years ago

r-priyam commented 3 years ago
from typing import Optional

import asyncpg

async def foo() -> Optional[asyncpg.Record]:  # errors here - Module cannot be used as a type
    ...

Current version - 2021.10.1 This used to work fine on version - 2021.10.0

erictraut commented 3 years ago

Thanks for the bug report. This was already reported in the pyright issue tracker, and I've implemented a fix. It will be included in the next release of pylance.

r-priyam commented 3 years ago

Thanks for the bug report. This was already reported in the pyright issue tracker, and I've implemented a fix. It will be included in the next release of pylance.

Cheers Eric, Thank you!

bschnurr commented 3 years ago

This issue has been fixed in version 2021.10.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2021102-20-october-2021

Heroyuki commented 2 years ago

Pylance version :v2021.12.2 "module cannot be used as a type" appears in List element type. image

erictraut commented 2 years ago

@Heroyuki, datetime is a module. I think you mean to reference the class, which is datetime.datetime.

Heroyuki commented 2 years ago

@erictraut Thanks, it solved. m( )m image

franchesoni commented 1 year ago
from PIL import Image
from torchvision.transforms.functional import to_tensor
import torch

def process_PIL(pil_img: Image) -> torch.Tensor:
    pil_img = pil_img.resize((224, 224))
    pil_img = pil_img.convert("RGB")  # needed to avoid grayscale
    return to_tensor(pil_img)

I get Module cannot be used as a typePylance[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues) (module) Image

the quick fix isn't useful, how do I solve this?

erictraut commented 1 year ago

@franchesoni, PIL.Image is a module, not a class. If you change your first line of code to from PIL.Image import Image, it will work fine.

prashant0708 commented 6 months ago
import os 
import sys 

class HousingException(Exception):

    def __init__(self,error_message:Exception,error_detail:sys):
        super().__init__(error_message)
        self.error_message=self.get_detailed_error_message(error_message=error_message,error_detail=error_detail)

    @staticmethod
    def get_detailed_error_message(error_message:Exception,error_detail:sys)->str:

        """
        error_message :Exception OBJECT
        error_details : object of sys module
        """

        _,_,exec_tb = error_detail.exc_info()

        line_number=exec_tb.tb_frame.f_lineno
        file_name= exec_tb.tb_frame.f_code.co_filename

        error_message = f"Error occured in scrip : [{file_name}] at line number : [{line_number}] error message :[{error_message }]"

        return error_message

    def __str__(self):
        return self.error_message

    def __repr__(self) ->str:
        return HousingException.__name__.str()

How to solve this

![Uploading custom exception.png…]()