# Sys "tb\_lineno" is not a known member of "None" in Pylance

**URL:** <https://discuss.python.org/t/sys-tb-lineno-is-not-a-known-member-of-none-in-pylance/26646>\
**Category:** Python Help\
**Created:** [May 10, 2023, 3:06pm UTC](https://discuss.python.org/t/sys-tb-lineno-is-not-a-known-member-of-none-in-pylance/26646 "2023-05-10T15:06:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredericomattos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fredericomattos/32/7219_2.png) [@fredericomattos](https://discuss.python.org/u/fredericomattos)\
**Post date:** [May 10, 2023, 3:06pm UTC](https://discuss.python.org/t/sys-tb-lineno-is-not-a-known-member-of-none-in-pylance/26646/1 "2023-05-10T15:06:16Z")

</div>

When creating my code, a Type Checking alert by Pylance is being issued when I try to generate an option to collect the error line in my code, as I want to save it in a log.

The code works, but as there is currently an incompatibility in use, I would like help finding a way to solve this incompatibility and keep the code from displaying this “error”.

> If there is no way to work around using `tb_lineno`, its ok show a  
> parallel indication that is accepted by Pylance without generating the  
> alert.

[![enter image description here](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/6/6a765160de0cf27d6eced1e55803fa43c77f46b9.png)](https://i.stack.imgur.com/Oj62b.png)

```lang-python
def log_exception(comments=''):
    logger.error(f"Line Error: {sys.exc_info()[2].tb_lineno}\nComments: {comments}", exc_info=True)

```

```lang-none
"tb_lineno" is not a known member of "None" Pylance
(property) tb_lineno: int

```

My complete code to make testing easier:

```lang-python
import logging
import sys
import os

ERROR_LOG_FILE = f'{os.path.splitext(os.path.basename( __file__ ))[0]}_errors.log'

def setup_logger(logger_name, log_file, level=logging.DEBUG):
    logger = logging.getLogger(logger_name)
    logger.setLevel(level)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = logging.FileHandler(log_file)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    return logger

def log_exception(comments=''):
    logger.error(f"Line Error: {sys.exc_info()[2].tb_lineno}\nComments: {comments}", exc_info=True)

logger = setup_logger( __name__ , ERROR_LOG_FILE)

def main():
    try:
        a = 1/0
    except Exception as e:
        log_exception()

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [May 10, 2023, 5:32pm UTC](https://discuss.python.org/t/sys-tb-lineno-is-not-a-known-member-of-none-in-pylance/26646/2 "2023-05-10T17:32:28Z")

</div>

The issue here is that `sys.exc_info()` may return a tuple of Nones if there is no active exception. Your `log_exception()` function would fail with an AttributeError in this case. It’s bad for error handling code to fail, so I’d recommend rewriting it as something like:

```python
def log_exception(comments=''):
    _, _, tb = sys.exc_info()
    if tb is not None:
        logger.error(f"Line Error: {tb.tb_lineno}\nComments: {comments}", exc_info=True)
    else:
        logger.error(f"log_exception() called without an active exception.\nComments: {comment}")

```

---

<div class="post-metadata">

**Author:** ![fredericomattos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fredericomattos/32/7219_2.png) [@fredericomattos](https://discuss.python.org/u/fredericomattos)\
**Post date:** [May 10, 2023, 5:52pm UTC](https://discuss.python.org/t/sys-tb-lineno-is-not-a-known-member-of-none-in-pylance/26646/4 "2023-05-10T17:52:05Z")

</div>

Thanks for support, @Jelle !
