Re: Common objects for CLI commands with Typer

2024-09-20 Thread Barry via Python-list


> On 20 Sep 2024, at 21:01, Loris Bennett via Python-list 
>  wrote:
> 
> Hi,
> 
> Apologies if the following description is to brief - I can expand if no
> one knows what I'm on about, but maybe a short description is enough.
> 
> I am developing a command line application using Typer.  Most commands
> need to do something in a database and also do LDAP stuff.  Currently
> each command creates its own Database and LDAP objects, since each
> command forms an entry point to the program.
> 
> With Typer, is there a way I can define the equivalent of class
> attributes at a single point which are then available to all commands?

I do not know typer. But the general solution is to create an instance of your 
class
and tell typer to call member function of the instance.

app = Application()
…
typer.set_callback(app.my_handler)

Barry

> 
> Cheers,
> 
> Loris
> 
> --
> This signature is currently under constuction.
> --
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble with mocking

2024-09-20 Thread Mark Bourne via Python-list

Norman Robins wrote:

I'm somewhat new to mocking for unit tests.

I have some code like this:

In foo/bar/baz.py I have 2 function I want to mock, one calls the other"
def function1_to_mock():
.
.
.

def function2_to_mock():
function1_to_mock()

In foo/bar/main.py I import 1 of these and call it"
from .baz import function2_to_mock

def some_function():
 function1_to_mock()


I'm assuming this is supposed to be calling `function2_to_mock`? 
(Otherwise the import should be for `function1_to_mock`, but then the 
fact `function2_to_mock` also calls `function1_to_mock` would be irrelevant)



 .
 .
 .

I want to mock both function1_to_mock and function2_to_mock

In my test I do this:

def function1_to_mock(kid):
 return MOCKED_VALUE

@pytest.fixture(autouse=True)
def mock_dependencies():
 with patch(foo.bar.baz.function1_to_mock') as mock_function1_to_mock, \
  patch('foo.bar.main.function2_to_mock') as mock_function2_to_mock:
 mock_function2_to_mock.return_value = {
 'this': 'that
 }
 yield mock_function1_to_mock, mock_function2_to_mock

def test_main(mock_dependencies):
 some_function()

When some_function is called the real function1_to_mock is called instead
of my mock.

Can someone please let me know how to properly mock these 2 functions.

Thanks!


In `foo/bar/main.py`, the line:
```
from .baz import function2_to_mock
```
creates a reference named `function2_to_mock` in `main.py` referring to 
the method in `foo/bar/baz.py`.


When you patch `foo.bar.baz.function2_to_mock`, you're patching the 
reference in `foo.bar.baz`, but the reference in `foo.bar.main` still 
refers to the original function object.


There are at least a couple of ways around this.  The way I prefer is to 
change `main.py` to import the `baz` module rather than just the function:

```
> from . import baz
>
> def some_function():
>  baz.function2_to_mock()
```
Here, `main.py` has a reference to the `baz` module rather than the 
individual function object.  It looks up `function2_to_mock` in `baz` 
just before calling it so, when the `baz` module is patched so that 
`baz.function2_to_mock` refers to a mock, the call in main.py` gets the 
mock and calls that rather than the original function.


There no memory saving by importing just the functions you need from 
`baz` - the whole module is still loaded on import and remains in 
memory, it's just that `main` only gets a reference to the one function. 
 The effect is similar to doing something like:

```
from . import baz
function2_to_mock = baz.function2_to_mock
del baz
```
...including the fact that, after the import, the reference to 
`function2_to_mock` in `main` is just a copy of the reference in `baz`, 
hence not getting updated by the patch.


The other way around it is to patch `main.function2_to_mock` instead of 
patching `foo.bar.baz.function2_to_mock`.


See also the documentation under "where to patch" at 
.


Note that, since you're patching `function2_to_mock`, you don't 
necessarily need to patch `function1_to_mock` as well.  The mock of 
`function2_to_mock` won't call `function1_to_mock` (or its mock) 
regardless of whether `function1_to_mock` has been patched, unless you 
set the mock of `function2_to_mock` to do so.  You don't necessarily 
need to patch `function1_to_mock`, unless of course there are other 
calls to it that you need to mock.


--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Bug in 3.12.5

2024-09-20 Thread Cameron Simpson via Python-list

On 20Sep2024 12:52, Martin Nilsson  wrote:

The attached program doesn’t work in 3.12.5, but in 3.9 it worked.


This mailing list discards attachments.

Please include your code inline in the message text.

Thanks,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


ANN: A new version (0.5.3) of python-gnupg has been released.

2024-09-20 Thread Vinay Sajip via Python-list
What Changed?=
This is an enhancement and bug-fix release, and all users are encouraged to 
upgrade.

Brief summary:

   
   - * Fix #117: Add WKD (Web Key Directory) support for auto-locating keys. 
Thanks to Myzel394 for the patch.

* Fix #237: Ensure local variable is initialized even when an exception occurs. 
  
   -
* Fix #239: Remove logging of decryption result.

This release [2] has been signed with my code signing key:

Vinay Sajip (CODE SIGNING KEY) 
Fingerprint: CA74 9061 914E AC13 8E66 EADB 9147 B477 339A 9B86

Recent changes to PyPI don't show the GPG signature with the download links.
An alternative download source where the signatures are available is at [4].
The source code repository is at [1].
Documentation is available at [5].

As always, your feedback is most welcome (especially bug reports [3],
patches and suggestions for improvement, or any other points via this group).

Enjoy!

Cheers

Vinay Sajip

[1] https://github.com/vsajip/python-gnupg
[2] https://pypi.org/project/python-gnupg/0.5.3
[3] https://github.com/vsajip/python-gnupg/issues
[4] https://github.com/vsajip/python-gnupg/releases/
[5] python-gnupg - A Python wrapper for GnuPG








-- 
https://mail.python.org/mailman/listinfo/python-list


Bug in 3.12.5

2024-09-20 Thread Martin Nilsson via Python-list
Dear Sirs !

The attached program doesn’t work in 3.12.5, but in 3.9 it worked.

Best Regards
Martin Nilsson
-- 
https://mail.python.org/mailman/listinfo/python-list


Common objects for CLI commands with Typer

2024-09-20 Thread Loris Bennett via Python-list
Hi,

Apologies if the following description is to brief - I can expand if no
one knows what I'm on about, but maybe a short description is enough.

I am developing a command line application using Typer.  Most commands
need to do something in a database and also do LDAP stuff.  Currently
each command creates its own Database and LDAP objects, since each
command forms an entry point to the program.

With Typer, is there a way I can define the equivalent of class
attributes at a single point which are then available to all commands?

Cheers,

Loris

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug in 3.12.5

2024-09-20 Thread Keith Thompson via Python-list
Martin Nilsson  writes:
> The attached program doesn’t work in 3.12.5, but in 3.9 it worked.

Attachments don't show up either on the mailing list or the newsgroup.

Try again with the program inline in your post (if it's not too long).

-- 
Keith Thompson (The_Other_Keith) keith.s.thompso...@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
-- 
https://mail.python.org/mailman/listinfo/python-list